如何在多维数组(PHP)中获取键值

时间:2017-03-27 13:17:12

标签: php arrays multidimensional-array

我是这样的阵列:

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和几个函数,但这不起作用。

3 个答案:

答案 0 :(得分: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);

答案 1 :(得分:0)

只需使用foreach循环:

$paths = array();

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

print_r($paths);

答案 2 :(得分:0)

@Adrien PAYEN只需使用如下所示的array_column(),通过它可以获取数组特定列的所有值:

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