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 ?

1
  • i think all the array functions can be useful for 1-D array. Commented Mar 30, 2012 at 5:29

3 Answers 3

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);
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

0

I am looking for answer like this

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

This works correct..

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.