Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all the key in multi-dimensional array in php

Array
(
    [0] => Array
        (
            [name] => A
            [id] => 1
            [phone] => 416-23-55
            [Base] => Array
                (
                    [city] => toronto
                )

            [EBase] => Array
                (
                    [city] => North York                
                )

            [Qty] => 1
        )

(
    [1] => Array
        (
            [name] => A
            [id] => 1
            [phone] => 416-53-66
            [Base] => Array
                (
                    [city] => qing
                )

            [EBase] => Array
                (
                    [city] => chong                
                )

            [Qty] => 2
        )

)

How can I get the all the key value with the format "0, name, id, phone, Base, city, Ebase, Qty"?

Thank you!

like image 545
aje Avatar asked Jun 27 '12 21:06

aje


People also ask

What is Array_keys () used for in PHP?

The array_keys() is a built-in function in PHP and is used to return either all the keys of and array or the subset of the keys. Parameters: The function takes three parameters out of which one is mandatory and other two are optional.

How do I find a key in an array?

PHP array_key_exists() Function The array_key_exists() function checks an array for a specified key, and returns true if the key exists and false if the key does not exist.

Can a Key have multiple values PHP?

To directly answer your question, no. PHP arrays can only contain one set of data for the key.


1 Answers

Try this

function array_keys_multi(array $array)
{
    $keys = array();

    foreach ($array as $key => $value) {
        $keys[] = $key;

        if (is_array($value)) {
            $keys = array_merge($keys, array_keys_multi($value));
        }
    }

    return $keys;
}
like image 56
Meliborn Avatar answered Oct 18 '22 15:10

Meliborn