SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: Find XML nodes

  1. #1
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,891
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Find XML nodes

    Hey,
    I have a PHP array and an XML file structured like below:
    Code XML:
    <?xml version="1.0"?>
    <question_list>
    	<question>
    		<id>
    			1
    		</id>
    		<text>
    			What is a Cat?
    		</text>
    	</question>
    	<question>
    		<id>
    			2
    		</id>
    		<text>
    			What is your name?
    		</text>
    	</question>
    	<question>
    		<id>
    			3
    		</id>
    		<text>
    			Where are you now?
    		</text>
    	</question>
    	<question>
    		<id>
    			5
    		</id>
    		<text>
    			Where is Brazil?
    		</text>
    	</question>
    </question_list>

    How can I search using SimpleXML through my xml file to find all questions which have id's which are in the PHP array?

    Thanks,

    Regards,
    Neil

  2. #2
    SitePoint Evangelist AlienDev's Avatar
    Join Date
    Feb 2007
    Location
    UK
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try something like this (untested):

    Code php:
    $ids = array(1,3,5);
    $result = array();
     
    $doc = simplexml_load_string('<your xml>');
     
    foreach ($doc->question_list as $question)
    {
        if (in_array((integer) $question->id, $ids))
        {
            $result[] = $question->name;
        }
    }
     
    return $result;
    Me on StackOverflow | Blog & personal website.

    I mostly use: PHP, Java, JavaScript, Android.

  3. #3
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,891
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey Thanks, I just about to understandit but could you just confirm the below:

    foreach ($doc->question_list as $question)

    The above is saying: "For each,, nope i'm already confused as to what and how this is working, ha ha ha, need soem help here.

    and

    if (in_array((integer) $question->id, $ids)) is saying:

    "If the id of a specific question is a a valid integer and is in the array called $ids thendo something?"

  4. #4
    SitePoint Evangelist AlienDev's Avatar
    Join Date
    Feb 2007
    Location
    UK
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    foreach: http://uk2.php.net/manual/en/control...es.foreach.php

    The first part ($doc->question_list) is always an array (or Iterable object, but forget about that just now).

    The second part is the name of a variable to be created.

    Each value in the array is put into the named variable, letting you do whatever you want with it.

    in_array: http://uk3.php.net/in_array

    First parameter is the "needle" and the second is the "haystick" (like the metaphor).

    The function looks inside the array for the needle.

    "(integer)" just casts the needle's value to an integer (e.g. from a string). It isn't necessary, probably.


    Does that help?
    Me on StackOverflow | Blog & personal website.

    I mostly use: PHP, Java, JavaScript, Android.

  5. #5
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,891
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok Thanks


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
  •