简体   繁体   English

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

[英]How to loop through multidimensional array and compare values?

I use the PHP script shown below that takes XML data from the Flickr API and gives out an array, but I don't know how to get the values from this array and do some operations with it. 我使用下面显示的PHP脚本,该脚本从Flickr API中获取XML数据并给出一个数组,但是我不知道如何从该数组中获取值并对其进行一些操作。

The array has this format: 数组具有以下格式:

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

Here is what I try to accomplish: 这是我要完成的工作:

I try to use these 2 values 我尝试使用这两个值
´[height_m] => 333 [width_m] => 500´ ´[height_m] => 333 [width_m] => 500´

and use the if construct.. 并使用if构造。

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

How do I get this construct in a for each loop? 如何在每个循环的a中获得此构造?

The code that generates the array comes from an awesome user at php.net 生成数组的代码来自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
}

The simplest approach I can conceive (change "$Object" to the variable name of your XMLElement Object): 我可以想到的最简单方法(将“ $ 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">;
    }
}

This is not a foreach loop but it will essentiall do the same thing: 这不是一个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);

Alternatively if you would prefer a foreach loop: 或者,如果您更喜欢一个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}";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM