我有這篇文章并在孩子內部獲取方法。$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 關注
- 191 瀏覽
添加回答
舉報
0/150
提交
取消