A.Developer
A.Developer

Reputation: 43

How to find the column key of Multidimensional Array?

Array
(
    [0] => Array
        (
            [A] => Demo 1
            [B] => Demo 2
            [C] => Demo 3
            [D] => Demo 4
        )

    [1] => Array
        (
            [A] => Sample 1
            [B] => Sample 2
            [C] => Sample 3
            [D] => Sample 4
        )
)

Find the column key like as (A,B,C,D)

Upvotes: 0

Views: 66

Answers (3)

Amine KOUIS
Amine KOUIS

Reputation: 1459

It's possible to do it by merging by array_merge and then extract the keys by array_keys

$res = array();
foreach($array as $sub) { 
    $res = array_merge($res, $sub); 
}   
$keys = array_keys($res);    
print_r($keys);

This will output:

Array
(
    [0] => A
    [1] => B
    [2] => C
    [3] => D
)

Upvotes: 2

Arif
Arif

Reputation: 6498

You can use foreach like this:

foreach ($stdTable as  $row) {
  echo "'".implode("','", array_keys($row))."'<br>".PHP_EOL;
}

OR

 foreach ($stdTable as  $row) {
     $keys='';
     foreach($row as $key => $value){
        $keys.= $key.',';
     }
     echo rtrim($keys,',');
 }

enter image description here

Upvotes: 0

olayemii
olayemii

Reputation: 600

Make use of array_keys() function on each array element

Manual here

Upvotes: 1

Related Questions