0

I want to loop through an object array and get id, category and name from it. Here is my php code. which is getting xml content from a server with a cURL request and then converting it to object array. I don't know how to loop through it and get all the values i want.

<?php 
    $url = "http://wingz.info/daas3b/search?q=word&top=&user=xxxx&pwd=xxxx";
    $ch = curl_init();
            curl_setopt ($ch, CURLOPT_URL, $url);
            curl_setopt ($ch, CURLOPT_HEADER, 0);
            ob_start();
            curl_exec ($ch);
            curl_close ($ch);
            $response = ob_get_clean();
            ob_end_clean();
    $response_array = @simplexml_load_string($response);

And the Object array is given below.

    SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [id] => search
        )

    [content] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [id] => 435
                        )

                    [category] => word
                    [name] => give
                )

            [1] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [id] => 436
                        )

                    [category] => word
                    [name] => verb
                )

            [2] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [id] => 437
                        )

                    [category] => word
                    [name] => noun
                )

            [3] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [id] => 438
                        )

                    [category] => word
                    [name] => person
                )

            [4] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [id] => 439
                        )

                    [category] => word
                    [name] => adjective
                )

            [5] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [id] => 440
                        )

                    [category] => word
                    [name] => adverb
                )))

2 Answers 2

1

This should list out the info you were asking for, and hopefully show you a bit about traversing the SimpleXML objects:

// Get the list of objects from your XML content
$objects_array = $response_array->content;

// Loop through all of the objects
foreach($objects_array as $xml_object) {
    echo '<br />' . xml_object->name; // The name
    echo '<br />' . xml_object->category; // The category

    $attributes = xml_object->attributes();
    echo '<br />' . $attributes['id']; // The id
}

Let me know if there was a problem and I'll edit this answer.

Edit: If it's not certain that each object will have an id/name/category: then the following, more robust code:

$objects_array = $response_array->content;

// Loop through all of the objects
foreach($objects_array as $xml_object) {
    echo '<br />' . (isset(xml_object->name) ? xml_object->name : ''); // The name
    echo '<br />' . (isset(xml_object->category) ? xml_object->category : ''); // The category

    $attributes = xml_object->attributes();
    echo '<br />' . (isset($attributes['id']) ? $attributes['id'] : ''); // The id
}

For more info on working with SimpleXML objects, check out https://secure.php.net/manual/en/simplexml.examples-basic.php.

Sign up to request clarification or add additional context in comments.

4 Comments

( ! ) Parse error: syntax error, unexpected '$xml_object' (T_VARIABLE) on line 15
Ah sorry, edited now to change "$objects_array-> as" to "$objects_array as"
Robust version done in response to "J. A. Streich's" robust code
Thanks man you help me a lot. Thank you very much.@Hiphop03199
1
// loop through
foreach($response_array->content as $thing)
{
  // get the id.
  $attrs = $thing->attributes();
  $id = $attrs[id];

  // check for and get the category
  if(isset($thing->category))
  {
    $category = $thing->category;
  }
  else
  {
    $category = '';
  }

  // check for and get the name.
  if(isset($thing->name))
  {
    $name = $thing->name;
  }
  else
  {
    $name = '';
  }
}

3 Comments

Aren't the 'id' and 'name' properties stored at different levels? Also: good work checking if they are set/providing default values, but might be cleaner to do with ternary: "$name = isset($thing->name) ? $thing->name : ''; ". A matter of personal preference though.
Oh, sorry, yeah the id is in attributes... hold on I'll edit... The ternary :? operator is one I use in real code, but often try to avoid when posting answers as it obfuscates the code.
Thanks for your time and efforts. It helps [email protected]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.