循环通过Object数组

时间:2016-02-17 14:55:52

标签: php arrays object

我想遍历一个对象数组并从中获取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);

下面给出了Object数组。

    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 个答案:

答案 0 :(得分: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 = '';
  }
}

答案 1 :(得分:1)

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

$array = [$size=6, $title='Guest',
          $field=[
            'join_two_fields' =>
              ['field1' => 'first_name','field2' => 'last_name'],
            'email', 'phone', 'company','address', 'comments'],
          $action='Create', $type='success'
         ];

foreach ($array as $item){

  if (is_array($item)){

    foreach ($item as $k => $v){

      if ($k === 'join_two_fields'){
        echo $v['field1'] . '<br>';
        echo $v['field2'] . '<br>';

      }
      else { echo $v . '<br>'; }

    }

  } else { echo $item . '<br>'; }
}

如果出现问题,请告诉我,我会编辑此答案。

编辑:如果不确定每个对象是否具有id / name / category:那么以下更强大的代码:

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

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