SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Evangelist ColinHughes's Avatar
    Join Date
    Sep 2004
    Location
    Spain
    Posts
    473
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need help with array

    Hi

    I have the following which puts the content of an XML feed into an array:

    PHP Code:
    <?php
    require_once('classes/xml2array.php');

        function 
    curlURL($url) {  
         
    $chcurl_init();  
           
    curl_setopt($chCURLOPT_URL$url);  
             
    curl_setopt($chCURLOPT_RETURNTRANSFERtrue);  
               
    curl_setopt($chCURLOPT_USERAGENT'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.2) Gecko/20070219 Firefox/2.0.0.2');  
           
    $output curl_exec($ch);  
          return 
    $output;  
       }  
        
      
    $curlResults curlURL("http://www.a website/75.xml");  
         
      
    $xml $curlResults;

    $converter = new Xml2Array();
    $converter->setXml($xml);
    $xml_array $converter->get_array();

    ?>
    The array is structured as follows:
    Code XML:
    Array
    (
        [root] => Array
            (
                [feed] => Array
                    (
                        [version] => Array
                            (
                                [#text] => 2_1
                            )
     
                    )
     
                [item] => Array
                    (
                        [0] => Array
                            (
                                [id] => Array
                                    (
                                        [#text] => HE 019
                                    )
     
                                [date] => Array
                                    (
                                        [#text] => 2009-02-01 21:17:05
                                    )
     
                                [ref] => Array
                                    (
                                        [#text] => HE 019
                                    )
     
                                [price] => Array
                                    (
                                        [#text] => 760000

    I would like some tailored advice as to how I can output the various values to my page. the example shows only one item there are around 400 items in the feed.

    Despite trawling the info about arrays and trying to implement what I have read I cannot seem to get my head around this.

    Can somebody please get me started on this.

    TIA

    Colin

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    foreach ($xml_array['root']['item'] as $item) {
      echo 
    $item['id']; 
      
    //...


  3. #3
    SitePoint Evangelist ColinHughes's Avatar
    Join Date
    Sep 2004
    Location
    Spain
    Posts
    473
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the reply.

    Your solution returns "ArrayArray" ?? I have also managed to get this as a result in one of my many attempts along with a few blank pages.


    C

  4. #4
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    echo $item['id']['#text']

    A little use of print_r or var_dump should make figuring out where you are in the array easy

  5. #5
    SitePoint Evangelist ColinHughes's Avatar
    Join Date
    Sep 2004
    Location
    Spain
    Posts
    473
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yep used print_r to view the array initially.

    PHP Code:
    $xml_array $converter->get_array();
    //print_r($xml_array);

    foreach ($xml_array['root']['item'] as $item) {
      echo 
    $item['id']['#text'];
      
    //...

    I was getting it wrong because I was trying to include the [0] in there in order to return individual item results. How would I go about doing this, adding it after [item] as seen in the array tree does not work?

    thanx

    C

  6. #6
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    The foreach would make $item equal to element 0, then 1, then 2...

    Does that not work? What do you see when you dump $item in that loop?

  7. #7
    SitePoint Evangelist ColinHughes's Avatar
    Join Date
    Sep 2004
    Location
    Spain
    Posts
    473
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes I see all items within the group.

    Maybe I failed to explain myself properly. What I will need to do is be able to direct a page to item say 120 and display only those details rather than just display a loop of all the items as at the moment.

    Thanks

    C

  8. #8
    SitePoint Evangelist ColinHughes's Avatar
    Join Date
    Sep 2004
    Location
    Spain
    Posts
    473
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code XML:
    Array
    (
        [root] => Array
            (
                [feed] => Array
                    (
                        [version] => Array
                            (
                                [#text] => 2_1
                            )
     
                    )
     
                [item] => Array
                    (
                        [0] => Array
                            (
                                [id] => Array
                                    (
                                        [#text] => HE 019
                                    )
     
                                [date] => Array
                                    (
                                        [#text] => 2009-02-01 21:17:05
                                    )
     
                                [ref] => Array
                                    (
                                        [#text] => HE 019
                                    )
                             )
     
                        [1] => Array
                            (
                                [id] => Array
                                    (
                                        [#text] => HE033
                                    )
     
                                [date] => Array
                                    (
                                        [#text] => 2009-03-05 14:48:22
                                    )
     
                                [ref] => Array
                                    (
                                        [#text] => HE33


    I am having difficulty understanding why [0] & [1] are seemingly ignored. These are the key elements in each item that will enable me to output unique content for an item. Do I really need to loop through the array or can I somehow just return the results relevant to whatever the value of the[0] element is?

    Colin
    Last edited by ColinHughes; May 8, 2009 at 04:13. Reason: added revised array

  9. #9
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    echo $xml_array['root']['item'][0][id]['#text']

  10. #10
    SitePoint Evangelist ColinHughes's Avatar
    Join Date
    Sep 2004
    Location
    Spain
    Posts
    473
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks

    I have actually got this far now, but I used ['id'] and not [id].

    This could get involved as I need to limit my list results to 3 and add some pagination. I also need to link from an item in the list to a new page featuring that item.

    Is there a way of counting the results to facilitate pagination

    Colin

  11. #11
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    count($xml_array['root']['item'])

    You might also consider parsing the xml array into a simpler array containing only the parts you want, right under the root level, if that'll make the rest of your code simpler.

  12. #12
    SitePoint Evangelist ColinHughes's Avatar
    Join Date
    Sep 2004
    Location
    Spain
    Posts
    473
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK Dan thanx for your help I have a lot to be going on with. Watch this space if I get stuck (sure I will)

    Thanx again

    Colin


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •