2017-03-27 46 views
0

我有一個數組這樣的:如何在multidimensionnal陣列(PHP)獲取鍵的值

array(2) { 
    ["dashboard"]=> 
    array(3) { 
    ["controller"]=> 
    string(5) "Index" 
    ["action"]=> 
    string(5) "index" 
    ["path"]=> 
    string(34) "dashboard/user/example/{page}/{id}" 
    } 
    ["home"]=> 
    array(3) { 
    ["controller"]=> 
    string(5) "Index" 
    ["action"]=> 
    string(6) "second" 
    ["path"]=> 
    string(10) "home/index" 
    } 
} 

如何獲得陣列中所有的「路徑」的價值觀? 我試圖在PHP中使用array_search和幾個函數,但這不起作用。

+1

你試過了什麼? – hassan

+0

你有什麼嘗試?你是什​​麼意思「不起作用」? – hering

+0

[我如何訪問數組/對象?](http://stackoverflow.com/questions/30680938/how-can-i-access-an-array-object) –

回答

1
foreach($arr as $key=>$val){ 
    $path[] = $val['path']; //store all paths into an array 
    //$path[$key] = $val['path']; //you can use this also to keep whose path is this 
} 
var_dump($path); 
0

只需使用一個foreach循環:

$paths = array(); 

foreach($array as $page) { 
    $paths[] = $page['path']; 
} 

print_r($paths); 
0

@Adrien PAYEN簡單地使用array_column()像下面,通過它可以得到所有值數組的特定列:

<?php 
    print_r(array_column($yourArray, "path"));