Re: Get all the keys from a hierarchical hash

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



use recursive calls to fetch them all in one array and there will be memory
cost.
refer array_keys function: http://php.net/manual/en/function.array-keys.php

not sure but this should be working.

function *get_keys_recursive*($arr) {
    while (list($k, $v) = each($arr)) {
       if (is_array($v) === true) {
             return array_keys(array_merge($v,* get_array_keys*($v)));
       } else {
             return array_keys($k);
       }
   }
}



On Thu, Jun 23, 2011 at 11:17 PM, Scott Baker <bakers@xxxxxxxxxxxx> wrote:

> I have a multi-tier hash (see below) and I'd like to be "search" the
> hash for a given $id, and return that section, regardless of how many
> layers deep it is. Sort of like how xpath works?
>
> Once I have that I'd like get ALL the children of a given node. So I
> could ask for 86, and get 36, 38, 56, etc and all THEIR children.
> Basically I want *all* the ID #s that are children.
>
> Array
> (
>    [88] => Array
>        (
>            [109] =>
>        )
>
>    [86] => Array
>        (
>            [36] => Array
>                (
>                    [8] =>
>                    [121] =>
>                    [127] =>
>                    [135] =>
>                    [144] =>
>                    [161] =>
>                    [165] =>
>                )
>
>            [38] => Array
>                (
>                    [18] =>
>                    [39] =>
>                    [156] =>
>                    [158] =>
>                    [182] =>
>                )
>
>            [56] =>
>            [97] =>
>            [107] => Array
>                (
>                    [240] =>
>                )
>
>            [115] =>
>            [123] =>
>            [146] =>
>            [149] =>
>            [223] =>
>        )
>
>    [157] => Array
>        (
>            [3] => Array
>                (
>                    [5] => Array
>                        (
>                            [11] =>
>                        )
>
>                    [13] => Array
>                        (
>                            [6] =>
>                            [7] =>
>                            [98] => Array
>                                (
>                                    [81] =>
>                                )
>
>                        )
>
>                    [111] => Array
>                        (
>                            [10] =>
>                            [17] =>
>                            [110] =>
>                        )
>
>                )
>
>            [148] => Array
>                (
>                    [9] =>
>                    [87] =>
>                    [102] =>
>                    [104] =>
>                    [114] =>
>                    [130] =>
>                    [133] =>
>                    [160] =>
>                    [201] =>
>                    [237] =>
>                    [238] =>
>                )
>
>        )
>
> )
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux