【问题标题】:How to loop through multidimensional array and compare values?如何遍历多维数组并比较值?
【发布时间】:2019-08-15 19:34:03
【问题描述】:

我使用下面显示的 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">;
 }

如何在 for each 循环中获取此构造?

生成数组的代码来自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
}

【问题讨论】:

  • 如果您使用原始 XML 并且不费心将其转换为数组,它可能会更快。如果您发布原始 XML,那么它可能会有所帮助。
  • 如果您得到了您需要的帮助,请将答案标记为正确。这会将问题标记为已回答,并有助于保持 Stack Overflow 干净整洁。如果没有一个答案对您有用,并且您找到了另一个解决方案,请将其作为答案提交并标记为正确。
  • 谢谢,我查过了,但我不太清楚如何使用这段代码。我已经了解了一些关于 php 的内容,但我认为这有点超出初学者的水平。我会将代码添加到我的问题中。有时我认为在 php 中学习和应用新事物的速度有多快,但我认为我在这里遗漏了一些东西,..
  • 请再看看他的@Fabian
  • @B.Richardson 这是一些简洁的代码,正如预期的那样,它创建了您在第一个代码块中向我们展示的 XmlElement 对象。但是,它并没有为您的问题提供任何新的启示。您是否尝试过您收到的任何答案?他们没有为你工作吗?你有错误吗?告诉我们出了什么问题,我们或许可以提供帮助。

标签: php arrays xml


【解决方案1】:

我能想到的最简单的方法(将“$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">;
    }
}

【讨论】:

    【解决方案2】:

    这不是一个 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}";
    }
    

    【讨论】:

      猜你喜欢
      • 2016-06-05
      • 1970-01-01
      • 2013-03-25
      • 1970-01-01
      • 1970-01-01
      • 2014-10-03
      • 2021-08-15
      • 2012-04-16
      相关资源
      最近更新 更多