如何在PHP中获取树样式数组的所有元素?

时间:2016-08-12 04:02:45

标签: php

这是一个简单的数组。我想取一对父母和母语儿童。我该怎么办?

$tree = array (
    1 => array (
            2 => "A",
            3 => "B",
            4 => array (
                    6 => "D",
                    7 => array (
                            8 => "E",
                            9 => array (
                                    10 => "F",
                                    11 => "G" 
                            ) 
                    ) 
            ),

            5 => "C" 
    ) 

);

2 个答案:

答案 0 :(得分:1)

使用递归函数

$tree = array (
    1 => array (
            2 => "A",
            3 => "B",
            4 => array (
                    6 => "D",
                    7 => array (
                            8 => "E",
                            9 => array (
                                    10 => "F",
                                    11 => "G" 
                            ) 
                    ) 
            ),

            5 => "C" 
    )
);

// Recursive function
// Take array as parameter
function print_array(array $array)
{
    // Loop through associative array in the form of key and value
    foreach($array as $key => $value)
    {
        // If the value is array data type call recursive function 
        // by passing the value itself
        if (gettype($value) == 'array')
        {
            echo "New array of Key:{$key} started <br>";
            print_array($value);
        }
        else
        {
            // If the value is not array just print it
            echo "Key: {$key}, Value {$value} <br>";
        }
    }
}

print_array($tree);

答案 1 :(得分:1)

以下是模式键1值的答案A键3值B键4值D,E,F,G键5值C

$tree = array (
    1 => array (
            2 => "A",
            3 => "B",
            4 => array (
                    6 => "D",
                    7 => array (
                            8 => "E",
                            9 => array (
                                    10 => "F",
                                    11 => "G" 
                            ) 
                    ) 
            ),

            5 => "C" 
    )
);

// Loop through $tree array
// Since you are assigning array to index 1, 
// Use $tree[1] instead of just $tree
foreach($tree[1] as $key => $value)
{
    // Pring key
    echo "Key {$key}: ";

    // Check key value type is array or not
    if (gettype($value) == 'array')
    {
        // if it's array data type call recursive function
        print_array($value);
    } 
    else
    {
        // just pring value
        echo "{$value}, ";
    }
    echo "\n";
}

// Recursive function
// Takes array as parameter
function print_array(array $array)
{
    // Loop through associative array in the form of key and value
    foreach($array as $key => $value)
    {
        // If the value is array data type or not
        if (gettype($value) != 'array')
        {
            // if true, just print the value
            echo "$value";
        }
        else
        {
            // call recursive function
            print_array($value);
        }
    }
}