DEVOPS
DEVOPS

Reputation: 18790

How to create comma separated string from using this kind of a two dimensional array?

I have a two dimensional array like this

$FrstArr = Array(
    [0]= array(
                [0]=>101,
                [1]=>ename1,
                [2]=>1110
            ),

    [1]= array(
                [0]=>102,
                [1]=>ename2,
                [2]=>1111
            ),

    [2]= array(
                [0]=>103,
                [1]=>ename3,
                [2]=>1112
             )
)

From this array I need to create one single dimensional array like this

$secondArr = array([0]=>1110,[1]=>1111,[2]=>1112);

With out using any loops how can I create $secondArr array using $FrstArr multidimensional array? Any php built in functionality is available for that?

Upvotes: 0

Views: 529

Answers (2)

KingCrunch
KingCrunch

Reputation: 131881

$secondArr = array_map(
  function ($item) { return $item[2]; },
  $firstArr
);

Worth to mention, that this will also (internally) loop over the array.

Upvotes: 0

deceze
deceze

Reputation: 522081

$secondArr = array_map(function ($i) { return $i[2]; }, $FrstArr);

This loops as well, but behind the scenes.

Upvotes: 1

Related Questions