Adrien PAYEN
Adrien PAYEN

Reputation: 27

How get keys values in a multidimensionnal array (PHP)

I've an array like that :

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"
  }
}

How get all "path" values in an array ? I tried to use array_search and several functions in PHP but this doesn't work.

Upvotes: 0

Views: 55

Answers (3)

lazyCoder
lazyCoder

Reputation: 2561

@Adrien PAYEN simply use array_column() like below by which you can get all values for a particular column of an array:

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

Upvotes: 0

Elbarto
Elbarto

Reputation: 1253

Simply use a foreach loop :

$paths = array();

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

print_r($paths);

Upvotes: 0

Bhaskar Jain
Bhaskar Jain

Reputation: 1691

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);

Upvotes: 1

Related Questions