views:

1092

answers:

5

I want to generate a list of the second level of keys used. Each record does not contain all of the same keys. But I need to know what all of the keys are. array_keys() doesn't work, it only returns a list of numbers.

Essentially the output Im looking for is:

action, id, validate, Base, Ebase, Ftype, Qty, Type, Label, Unit

I have a large multi-dimensional array that follows the format:

Array
(
    [0] => Array
        (
            [action] => A
            [id] => 1
            [validate] => yes
            [Base] => Array
                (
                    [id] => 2945
                )

            [EBase] => Array
                (
                    [id] => 398
                )

            [Qty] => 1
            [Type] => Array
                (
                    [id] => 12027
                )

            [Label] => asfhjaflksdkfhalsdfasdfasdf
            [Unit] => asdfas
        )

    [1] => Array
        (
            [action] => A
            [id] => 2
            [validate] => yes
            [Base] => Array
                (
                    [id] => 1986
                )

            [FType] => Array
                (
                    [id] => 6
                )

            [Qty] => 1
            [Type] => Array
                (
                    [id] => 13835
                )

            [Label] => asdssdasasdf
            [Unit] => asdger
        )
)

Thanks for the help!

+3  A: 
foreach($bigArray as $array){    
    foreach($array as $key=>$value){
        echo $key;
    }
}

That should do what you want.

inkedmn
thanks inkedmn - I had first attempted your first version (prior to your edit) and it obviously didn't work. This one worked just fine, then a simple if statement to check for duplicates and I had my data.
+3  A: 

What about something like this :

$your_keys = array_keys($your_array[0]);

Of course, this is considering all sub-arrays have the same keys ; in this case, you only need the keys of the first sub-array (no need to iterate over all first-level sub-arrays, I guess)


And, as a shortened / simplified example :

$your_array = array(
    array(
        'action' => 'A',
        'id' => 1,
        'base' => array('id' => 145),
    ),
    array(
        'action' => 'B',
        'id' => 2,
        'base' => array('id' => 145),
    ),
    array(
        'action' => 'C',
        'id' => 3,
        'base' => array('id' => 145),
    )
);

$your_keys = array_keys($your_array[0]);
var_dump($your_keys);

Will get you :

array
  0 => string 'action' (length=6)
  1 => string 'id' (length=2)
  2 => string 'base' (length=4)

You can the use implode to get the string you asked for :

echo implode(', ', $your_keys);

will get you :

action, id, base

ie, the list of the keys of the first sub-array.

Pascal MARTIN
Thanks Pascal unfortunately each array does not contain the same keys.
Ho, too bad :-( In this case, what about using array_keys on each sub-array, and, then, array_merge on the lists of keys of those sub-arrays ?
Pascal MARTIN
+1  A: 
array_keys(call_user_func_array('array_merge', $a));

Merge all values and retrieve the resulting keys.

Øystein Riiser Gundersen
+2  A: 
<?php

// Gets a list of all the 2nd-level keys in the array
function getL2Keys($array)
{
    $result = array();
    foreach($array as $sub) {
        $result = array_merge($result, $sub);
    }        
    return array_keys($result);
}

?>

edit: removed superfluous array_reverse() function

jcinacio
Works great, no need to check values for repeated keys!
Why the call to `array_reverse()`?
Øystein Riiser Gundersen
@gunderwonder - it turns the keys into values - so that array_merge() will merge all the keys together
jcinacio
`array_merge()` merges any non-numeric keys shared between the arrays and `array_reverse()` just reverses the order of the keys. Your function works just as well without `array_reverse()`
Øystein Riiser Gundersen
@gunderwonder - you are right - probably time to turn on the coffee machine...
jcinacio
A: 
kinderjit singh