14

Here's a section my multidimensional array:

Array ( 
[0] => Array ( [0] => Height [1] => 40 ) 
[1] => Array ( [0] => Weight [1] => 15 ) 
[2] => Array ( [0] => Ctr_Percent [1] => 15 ) 
) 

What would the syntax be for just printing height, weight, and ctr_percent? I don't mean echoing it like:

echo $array[0][0];
echo $array[1][0];

Is there a way to iterate through the entire multidimensional array and echo out the first value of each child array?

Norse
  • 5,674
  • 16
  • 50
  • 86

7 Answers7

22

Supposing you use php 5.3:

$first_elements = array_map(function($i) {
    return $i[0];
}, $data);

Otherwise you need to implement a callback function or just use plain old foreach

zerkms
  • 249,484
  • 69
  • 436
  • 539
  • I think it should be noted that that works only if index 0 happens to be the key of the first element in the array. It simply doesn't return the first value of the subarray it just returns the value of the element with key 0 which might be the first element. – Christoph Diegelmann Dec 06 '16 at 15:36
  • @Christoph the element with the index 0 of a numerically indexed array is semantically "the first value of the subarray". If the nested arrays are not numerically indexed - there is really no much sense to speak about their "order" at all. – zerkms Dec 06 '16 at 19:20
  • php has an order for each array independant of it's keys e.g. in [1=>5,0=>3] the array in numerically index yet still key 1 is the first element not zero. You will always iterate over this implicit order using foreach not in sorted order of the keys. There are no plain arrays like in c like languages. – Christoph Diegelmann Dec 07 '16 at 09:34
17

Here is a one-liner:

array_map('array_shift', $array);

Will return:

Array
(
    [0] => Height
    [1] => Weight
    [2] => Ctr_Percent
)

And here is another one:

array_combine(array_map('array_shift', $temp), array_map('array_pop', $temp))

Will return:

Array
(
    [Height] => 40
    [Weight] => 15
    [Ctr_Percent] => 15
)
Shumoapp
  • 1,489
  • 16
  • 15
6

Use array_column:

$result = array_column($array, 0);
Robo Robok
  • 21,132
  • 17
  • 68
  • 126
1
foreach ($main_array as $inner_array){
  echo $inner_array[0] . "\n";
}
Lucas Zamboulis
  • 2,494
  • 5
  • 24
  • 27
John Conde
  • 217,595
  • 99
  • 455
  • 496
1
foreach($array as $x) {
   echo $x[0]."\n";
}
gopi1410
  • 6,567
  • 9
  • 41
  • 75
1

I think the function your looking for is reset() e.g.

array_map('reset', $array);

or

foreach ($array as $subarray)
    echo reset($subarray)."\n";

Note that this works even if 0 is not the first index of the array. E.g. $a = [1=>5,0=>3]; echo reset($a); would still echo 5;.

Christoph Diegelmann
  • 2,004
  • 15
  • 26
1

The easiest way to do it using array_walk

        function getFirstElement(&$val){
            $val = $val[0];
        }
        array_walk($data,'getFirstElement');

Now if you print $data like print_r($data); you will get result as below

Array
(
   [0] => Height
   [1] => Weight
   [2] => Ctr_Percent
)
Omar
  • 901
  • 11
  • 14