Research and development



  •   <?php
        $arr = Array(
            254362 => Array(
                254370,
                254363
            ),
            254363 => Array(
                254369
            ),
            254357 => Array(
                254362,
                254359
            ),
            254359 => Array(
                254360
            ),
            Array(
                254357
            )
        );
    

    There's a mass, we need to circumvent and create a new one, e.g., in the selection of key 254362, the values are 254370, 254363 and the values obtained as keys again, etc.

    The new body should have a structure like an example, but without untouched keys in searching.

    There's a code now:

        var_dump(get_childs($child_array, $id));

    function get_childs($array, $id) {
    foreach ($array[$id] as $value) {
    $childs[$id] = $array[$id];
    get_childs($array, $value);
    }
    return $childs;
    }

    The performance makes a mistake ERR_CONNECTION_RESET



  • Your procedure may be focused on data in which 2 id will point to each other. You don't see any of these in your example, but you have to take into account that there is an element in your body that has no may mislead:

        254363 => Array( 254369 ),
        ...
        254359 => Array( 254360 ),
        Array( 254357 ) <-- Вы думаете это 0й элемент ?
                            НЕТ - это следующий по номеру, за максимальным в вашем массиве
                            Т.е. 254364
    

    A procedure with cycle protection and other errors, type of missing body elements and not declared variable childs should look like this:

    <?php
     $arr = Array(
            254362 => Array( 254370,  254363 ), 254363 => Array( 254369  ),
            254357 => Array( 254362, 254359 ), 254359 => Array( 254360 ),
            254369 => Array( 253000 ), Array( 254357 )
        );
    

    var_dump(get_childs($arr, 254362));

    function get_childs(&$array, $id) {
    static $childs=[];
    static $allkey=[];
    if(!array_key_exists($id,$array)) return false;
    foreach ($array[$id] as $value) {
    $childs[$id] = $array[$id];
    if(!in_array($value,$allkey)) {
    $allkey[]=$value;
    get_childs($array, $value);
    }
    }
    return $childs;
    }
    ?>


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2