0

Possible Duplicate:
How to “flatten” a multi-dimensional array to simple one in PHP?

How to create an array of inner array values with array function ?

Here is array

Array
(
    [0] => Array
    (
        [Detail] => Array
        (
            [detail_id] => 1
        )

    )

    [1] => Array
    (
        [Detail] => Array
        (
            [detail_id] => 4
        )

    )

)

I want create an array with detail_id of above array i.e

array(1, 4)

Is it done any array function in PHP ?

Community
  • 1
  • 1
Justin John
  • 9,223
  • 14
  • 70
  • 129

3 Answers3

1

There isn't any one function which can do this single-handed. array_map is the closest you'd get, but it doesn't really deal with the extra recursion level. Why don't you just use loops?

$new_array = array();

foreach($main_array as $key => $second_array)
{
   foreach($second_array as $second_key => $third_array)
   {
      $new_array[] = $third_array['detail_id'];
   }
}

echo implode(',',$new_array);
hohner
  • 11,498
  • 8
  • 49
  • 84
0

If your are looking for a one liner:

$val = array(
   array('Detail' => array('detail_id' => 1)),
   array('Detail' => array('detail_id' => 4)),
 );

var_dump($val);
BetaRide
  • 16,207
  • 29
  • 99
  • 177
0

I am looking for answer like this

$result = array_map( function($a) {
    return $a['Detail']['detail_id']; }, $mainArray
);

This works correct..

Justin John
  • 9,223
  • 14
  • 70
  • 129