Php 如何循环多维数组并比较值?

Php 如何循环多维数组并比较值?,php,arrays,xml,Php,Arrays,Xml,我使用下面显示的PHP脚本,它从Flickr API获取XML数据并给出一个数组,但我不知道如何从这个数组中获取值并使用它执行一些操作 数组具有以下格式: XmlElement Object ( [name] => rsp [attributes] => Array ( [stat] => ok ) [content] => [children] => Array

我使用下面显示的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] => 
                                )

以下是我试图实现的目标:

我尝试使用这两个值
'[高度m]=>333[宽度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">;
 }

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

foreach($Object->children[0]->childrenas$child){
如果($child->attributes['width\u m']/$child->attributes['height\u m']>=1.25){
echo“attributes['url_m']”width=“100”>;
}
否则{
echo“attributes['url_m']”width=“50”>;
}
}

这不是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);

如果您使用原始XML,而不必费心将其转换为数组,可能会更快。如果您发布原始XML,那么可能会有所帮助。如果您获得了所需的帮助,请将答案标记为正确。这会将问题标记为已回答,并有助于保持堆栈溢出整洁。如果没有一个答案对您有效,并且您找到了另一个解决方案,请将其作为答案提交并标记为正确。谢谢,我检查了它,但我不太知道如何使用此代码。关于php,我已经了解了一些东西,但我认为这有点超出了初学者的水平。我将把代码添加到我的问题中。有时候,我觉得在php中学习和应用新事物的速度是如此之快,真是太棒了,但我觉得我在这里遗漏了一些东西,…请再看看他的@Fabian@B.Richardson这是一些简洁的代码,正如预期的那样,它创建了您在第一个代码块中向我们展示的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">;
    }
}
$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($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}";
}