如何遍历多维数组并比较值?

时间:2019-03-25 14:15:31

标签: php arrays xml

我使用下面显示的PHP脚本,该脚本从Flickr API中获取XML数据并给出一个数组,但是我不知道如何从该数组中获取值并对其进行一些操作。

数组具有以下格式:

XmlElement Object
(
    [name] => rsp
    [attributes] => Array
        (
            [stat] => ok
        )

    [content] => 
    [children] => Array
        (
            [0] => XmlElement Object
                (
                    [name] => photos
                    [attributes] => Array
                        (
                            [page] => 1
                            [pages] => 13751
                            [perpage] => 100
                            [total] => 1375086
                        )

                    [content] => 
                    [children] => Array
                        (
                            [0] => XmlElement Object
                                (
                                    [name] => photo
                                    [attributes] => Array
                                        (
                                            [id] => 25000430521
                                            [owner] => 73422502@N08
                                            [secret] => 19459b26e4
                                            [server] => 1703
                                            [farm] => 2
                                            [title] => Health
                                            [ispublic] => 1
                                            [isfriend] => 0
                                            [isfamily] => 0
                                            [url_m] => https://farm2.staticflickr.com/1703/25000430521_19459b26e4.jpg
                                            [height_m] => 500
                                            [width_m] => 500
                                        )

                                    [content] => 
                                    [children] => 
                                )

                            [1] => XmlElement Object
                                (
                                    [name] => photo
                                    [attributes] => Array
                                        (
                                            [id] => 35305743196
                                            [owner] => 73422502@N08
                                            [secret] => 9601255217
                                            [server] => 4232
                                            [farm] => 5
                                            [title] => Health
                                            [ispublic] => 1
                                            [isfriend] => 0
                                            [isfamily] => 0
                                            [url_m] => https://farm5.staticflickr.com/4232/35305743196_9601255217.jpg
                                            [height_m] => 333
                                            [width_m] => 500
                                        )

                                    [content] => 
                                    [children] => 
                                )

这是我要完成的工作:

我尝试使用这两个值
´[height_m] => 333 [width_m] => 500´

并使用if构造。

 if (**width_m** / **height_m** >= 1.25 ) { 
 echo "<img src=" **url_m** " width="100">;
 }
 else {
 echo "<img src=" **url_m** " width="50">;
 }

如何在每个循环的a中获得此构造?

生成数组的代码来自php.net的一位了不起的用户

class XmlElement {
  var $name;
  var $attributes;
  var $content;
  var $children;
};

function xml_to_object($xml) {
  $parser = xml_parser_create();
  xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
  xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
  xml_parse_into_struct($parser, $xml, $tags);
  xml_parser_free($parser);

  $elements = array();  // the currently filling [child] XmlElement array
  $stack = array();
  foreach ($tags as $tag) {
    $index = count($elements);
    if ($tag['type'] == "complete" || $tag['type'] == "open") {
      $elements[$index] = new XmlElement;
      $elements[$index]->name = $tag['tag'];
      $elements[$index]->attributes = $tag['attributes'];
      $elements[$index]->content = $tag['value'];
      if ($tag['type'] == "open") {  // push
        $elements[$index]->children = array();
        $stack[count($stack)] = &$elements;
        $elements = &$elements[$index]->children;
      }
    }
    if ($tag['type'] == "close") {  // pop
      $elements = &$stack[count($stack) - 1];
      unset($stack[count($stack) - 1]);
    }
  }
  return $elements[0];  // the single top-level element
}

2 个答案:

答案 0 :(得分:0)

我可以想到的最简单的方法(将“ $ Object”更改为XMLElement对象的变量名):

foreach($Object->children[0]->children as $child) {
    if ($child->attributes['width_m'] / $child->attributes['height_m'] >= 1.25 ) { 
        echo "<img src=" . $child->attributes['url_m'] . " width="100">;
    }
    else {
        echo "<img src=" . $child->attributes['url_m'] . " width="50">;
    }
}

答案 1 :(得分:0)

这不是一个foreach循环,但必将做同样的事情:

$allImageHtml = array_map(function($photoData){
    $width = ($photoData->attributes['width_m'] / $photoData->attributes['height_m']) >= 1.25 ?100:50;
    return "<img src='{$child->attributes['url_m']} width='{$width}'/>";
}, $Object->children[0]->children);

echo implode('<!-- Separator HTML. (ie: br) -->', $allImageHtml);

或者,如果您希望使用foreach循环,则:

foreach($Object->children[0]->children as $photoData){
    $width = ($photoData->attributes['width_m'] / $photoData->attributes['height_m']) >= 1.25 ? 100: 50;
    $spacer = "<!-- Separator HTML. (ie: br) -->";
    echo "<img src='{$child->attributes['url_m']} width='{$width}'/>{$spacer}";
}