我有这篇文章并在孩子内部获取方法。$array1 = [ "attribute" => "MySchool", "child" => [ "method" => "GET", "child" => [ "attribute" => "school", "child" => [ [ "attribute" => "harvard" ], ], ], ], [ "method" => "POST", "child" => [ "attribute" => "school", "child" => [ [ "attribute" => "stanford" ], ], ], ],]$array2 = array( 0 => "GET" 1 => "school" 2 => "harvard");现在我只想要方法get和它的属性值。所以我想要一个像这样的数组结果:array(0 => "MySchool"1 => "get"2 => "school"3 => "harvard")
3 回答
MMTTMM
TA贡献1869条经验 获得超4个赞
您可以检查 'method' 的键值是否为 GET,然后提取您需要的元素。
$result = [];
foreach ($array1 as $key => $value) {
if ($key === 'attribute') $result[] = $value;
if ($key === 'child' && $value['method'] === 'GET') {
$result[] = $value['method'];
$result[] = $array1['child']['child']['attribute'];
$result[] = $array1['child']['child']['child'][0]['attribute'];
}
}
print_r($result);
/*
* Output:
* Array
* (
* [0] => MySchool
* [1] => GET
* [2] => school
* [3] => harvard
* )
*/
- 3 回答
- 0 关注
- 175 浏览
添加回答
举报
0/150
提交
取消