循环遍历对象数组


Looping through an Object array

我想遍历一个对象数组并从中获取id, categoryname。这是我的 php 代码。它是使用 cURL 请求从服务器获取 XML 内容,然后将其转换为对象数组。我不知道如何遍历它并获得我想要的所有值。

<?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);

下面给出了对象数组。

    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
                )))

这应该列出您要求的信息,并希望向您展示一些有关遍历 SimpleXML 对象的信息:

// 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
}

如果有问题,请告诉我,我将编辑此答案。

编辑:如果不确定每个对象都有一个id/名称/类别:那么以下更健壮的代码:

$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
}

有关使用 SimpleXML 对象的详细信息,请查看 https://secure.php.net/manual/en/simplexml.examples-basic.php。

// 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 = '';
  }
}