php - php获取多维数组中的特定数组
问题描述
我正在寻找从多维数组中获取数组的解决方案
例如; 这是我的数组:
Array
(
[1] => Array
(
[id] => 2
[name] => Laïla Mertens
[text] => Laïla Mertens
[parent_id] =>
[href] => http://google.com
[nodes] => Array
(
[0] => Array
(
[id] => 1
[name] => Erwin Dubois
[text] => Erwin Dubois
[parent_id] => 2
[href] => http://google.com
[nodes] => Array
(
[0] => Array
(
[id] => 3
[name] => Test Gebruiker
[text] => Test Gebruiker
[parent_id] => 1
[href] => http://google.com
)
[1] => Array
(
[id] => 4
[name] => Catharina
[text] => Catharina
[parent_id] => 1
[href] => http://google.com
)
)
)
[1] => Array
(
[id] => 5
[name] => Araxanta
[text] => Araxanta
[parent_id] => 2
[href] => http://google.com
[nodes] => Array
(
[0] => Array
(
[id] => 15
[name] => Kevin
[text] => Kevin
[parent_id] => 5
[href] => http://google.com
[nodes] => Array
(
[0] => Array
(
[id] => 16
[name] => Petra
[text] => Petra
[parent_id] => 15
[href] => http://google.com
)
[1] => Array
(
[id] => 17
[name] => Shannah
[text] => Shannah
[parent_id] => 15
[href] => http://google.com
)
)
)
)
)
[2] => Array
(
[id] => 6
[name] => Christian
[text] => Christian
[parent_id] => 2
[href] => http://google.com
[nodes] => Array
(
[0] => Array
(
[id] => 13
[name] => Macy VanMaele
[text] => Macy VanMaele
[parent_id] => 6
[href] => http://google.com
)
[1] => Array
(
[id] => 14
[name] => April Poizat
[text] => April Poizat
[parent_id] => 6
[href] => http://google.com
[nodes] => Array
(
[0] => Array
(
[id] => 7
[name] => Sigrid
[text] => Sigrid
[parent_id] => 14
[href] => http://google.com
)
)
)
)
)
[3] => Array
(
[id] => 8
[name] => Nikita
[text] => Nikita
[parent_id] => 2
[href] => http://google.com
)
[4] => Array
(
[id] => 9
[name] => Salina Lemmens
[text] => Salina Lemmens
[parent_id] => 2
[href] => http://google.com
)
[5] => Array
(
[id] => 10
[name] => Emmily Polen
[text] => Emmily Polen
[parent_id] => 2
[href] => http://google.com
)
[6] => Array
(
[id] => 11
[name] => Jitka Symus
[text] => Jitka Symus
[parent_id] => 2
[href] => http://google.com
)
[7] => Array
(
[id] => 12
[name] => Amina Gigovic
[text] => Amina Gigovic
[parent_id] => 2
[href] => http://google.com
)
)
)
)
我想得到 id 等于 1 的数组
所以在这个例子中, [id] => 1 我想捕获 id 1 的整个数组,因此:
Array
(
[0] => Array
(
[id] => 1
[name] => Erwin Dubois
[text] => Erwin Dubois
[parent_id] => 2
[href] => http://google.com
[nodes] => Array
(
[0] => Array
(
[id] => 3
[name] => Test Gebruiker
[text] => Test Gebruiker
[parent_id] => 1
[href] => http://google.com
)
[1] => Array
(
[id] => 4
[name] => Catharina
[text] => Catharina
[parent_id] => 1
[href] => http://google.com
)
)
)
php中有没有一种方法可以让我拥有类似的功能;
function get_subarray($id) {
// Select array where id = $id
}
任何指向该方向的点都非常感谢!函数中需要它,因为我会根据其唯一 ID 解析不同的用户级别
大批:
$json = file_get_contents("https://innerbeauthe.be/cp/_data/fetch.php");
$data = json_decode($json,true);
解决方案
您可以定义一个遍历数组以搜索所需数组键的递归函数。
虽然 PHP 包含一个函数array_walk_recursive()
,但它会遍历叶子节点,并且很难返回父数组。同样,使用RecursiveArrayIterator
实际上使获取您想要的数组而不是叶子笔记变得复杂。相反,创建自己的并不太复杂:
// Function accepts an array and a search value
function walk_for_array(array $a, $search) {
// Loop over the array
foreach ($a as $k => $v) {
// Find sub-arrays (rather than scalar values)
if (is_array($v)) {
// Check if there is an 'id' key and if it matches your search value
if (isset($v['id']) && $v['id'] == $search) {
// return it
return $v;
}
else {
// Otherwise continue drilling into the array for more arrays
$sub = walk_for_array($v, $search);
// Return the recursive value only if the inner call
// was non-null
if ($sub) return $sub;
}
}
}
}
// Call it, searching for id == 1
$found = walk_for_array($your_big_array, 1);
print_r($found);
// Call it, searching for id == 13
$found = walk_for_array($your_big_array, 13);
print_r($found);
印刷:
Array
(
[id] => 1
[name] => Erwin Dubois
[text] => Erwin Dubois
[parent_id] => 2
[href] => http://google.com
[nodes] => Array
(
[0] => Array
(
[id] => 3
[name] => Test Gebruiker
[text] => Test Gebruiker
[parent_id] => 1
[href] => http://google.com
)
[1] => Array
(
[id] => 4
[name] => Catharina
[text] => Catharina
[parent_id] => 1
[href] => http://google.com
)
)
)
对于 id = 13
Array
(
[id] => 13
[name] => Macy VanMaele
[text] => Macy VanMaele
[parent_id] => 6
[href] => http://google.com
)
推荐阅读
- javascript - TypeError:无法读取未定义的属性(读取“地图”)。当我尝试映射一组加密货币时引发错误
- nft - Solana Metaplex Candy Machine `tokenMint` 元数据为空
- python - Dockerfile 命令不在 compose 上运行,但在构建容器后在 CLI 中运行
- python - 错误:无法卸载“PyGObject”。这是一个 distutils 安装的项目
- swiftui - SwiftUI - 更新 Firebase 实时数据库上的数据
- python - 如何使用 python 将包含超链接 .docx 的文档合并到另一个 .docx?
- javascript - 如何将下面的 JS 代码转换为 ES6 类代码
- html - 怎么把图标放进去
- 标签?
- flutter - 缩小的 ListView 被截断
- c# - 如何使用 json.net 序列化和反序列化多态列表,同时保留继承的对象类型?