简体   繁体   中英

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)

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
)

Make use of array_keys() function on each array element

Manual here

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,',');
 }

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM