gpt4 book ai didi

php - 在递归数组中找到正确的值

转载 作者:行者123 更新时间:2023-12-02 07:52:53 25 4
gpt4 key购买 nike

我有一个包含多个子数组的数组,如下所示:

    Array
(
[0] => Array
(
[Page_ID] => 1
[Page_Parent_ID] => 0
[Page_Title] => Overview
[Page_URL] => overview
[Page_Type] => content
[Page_Order] => 1
)

[1] => Array
(
[0] => Array
(
[Page_ID] => 2
[Page_Parent_ID] => 1
[Page_Title] => Team
[Page_URL] => overview/team
[Page_Type] => content
[Page_Order] => 1
)

)

[2] => Array
(
[Page_ID] => 3
[Page_Parent_ID] => 0
[Page_Title] => Funds
[Page_URL] => funds
[Page_Type] => content
[Page_Order] => 2
)

[3] => Array
(
[0] => Array
(
[Page_ID] => 4
[Page_Parent_ID] => 3
[Page_Title] => Strategy
[Page_URL] => funds/strategy
[Page_Type] => content
[Page_Order] => 1
)

[1] => Array
(
[0] => Array
(
[Page_ID] => 7
[Page_Parent_ID] => 4
[Page_Title] => A Class Fund
[Page_URL] => funds/strategy/a-class-fund
[Page_Type] => content
[Page_Order] => 1
)

[1] => Array
(
[0] => Array
(
[Page_ID] => 10
[Page_Parent_ID] => 7
[Page_Title] => Information
[Page_URL] => funds/strategy/a-class-fund/information
[Page_Type] => content
[Page_Order] => 1
)

[1] => Array
(
[Page_ID] => 11
[Page_Parent_ID] => 7
[Page_Title] => Fund Data
[Page_URL] => funds/strategy/a-class-fund/fund-data
[Page_Type] => content
[Page_Order] => 2
)

)

[2] => Array
(
[Page_ID] => 8
[Page_Parent_ID] => 4
[Page_Title] => B Class Fund
[Page_URL] => funds/strategy/b-class-fund
[Page_Type] => content
[Page_Order] => 2
)

我需要一个函数来找到正确的 Page_URL,所以如果你知道 $url 是“funds/strategy/a-class-fund”,我需要将它传递给一个返回单个数组结果的函数(这将是Page_ID = 7 array in this example)。

今天有点愚蠢,任何帮助将不胜感激!

最佳答案

RecursiveArrayIterator救援:

function findByPageUrl($url, array $data) {

$iterator = new RecursiveIteratorIterator(
new RecursiveArrayIterator($data),
RecursiveIteratorIterator::SELF_FIRST);

foreach($iterator as $val) {
if(is_array($val) && array_key_exists('Page_URL', $val)) {
if($val['Page_URL'] === $url) {
return $val;
}
}
}
return FALSE;
}

关于php - 在递归数组中找到正确的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2653368/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com