簡體   English   中英

如何從復雜的多維數組中獲取所有值?

[英]How to get all values from complex multi-dimensional array?

我有以下多維數組

 $sample = array( '1232' => 'Nokia 72', '234' => array( '534' => 'Samsung 58', '345' => 'Samsung 64' ), '3445' => 'Micromax 1542c', '542' => array( '4645' => 'LG 58', '5765' => 'LG 64' ) ); 

現在,我只想收集每個部分的每個值的值。

我的輸出應如下所示

 Array ( [0] => Nokia 72 [1] => Samsung 58 [2] => Samsung 64 [3] => Micromax 1542c [4] => LG 58 [5] => LG 64 ) 

我不想用foreach函數。

您可以通過使用

 array_walk_recursive($sample, create_function('$val, $key, $obj', 'array_push($obj, $val);'), &output); 

您還可以從下面的鏈接中獲取更多參考

如何對濾波器只值從-復多維陣列

$sample = array(
        '1232' => 'Nokia 72',
        '234' => array(
                    '534' => 'Samsung 58',
                    '345' => 'Samsung 64'
                  ),
        '3445' => 'Micromax 1542c',
        '542' => array(
                    '4645' => 'LG 58',
                    '5765' => 'LG 64'
                  )
      );

array_walk_recursive($sample, function($a) use (&$return) { $return[] = $a; });

var_dump($return);

輸出:

array(6) { [0]=> string(8) "Nokia 72" [1]=> string(10) "Samsung 58" [2]=> string(10) "Samsung 64" [3]=> string(14) "Micromax 1542c" [4]=> string(5) "LG 58" [5]=> string(5) "LG 64" } 

這是帶有演示的PHP沙箱: http : //sandbox.onlinephpfunctions.com/

該解決方案使用array_walk_recursive()和PHP匿名函數。

http://php.net/array_walk_recursive

http://php.net/manual/en/functions.anonymous.php

RecursiveIteratorIterator在與iterator_to_array函數一起使用時會返回一個展平的數組。

Demo

$sample = array(
        '1232' => 'Nokia 72',
        '234' => array(
                    '534' => 'Samsung 58',
                    '345' => 'Samsung 64'
                  ),
        '3445' => 'Micromax 1542c',
        '542' => array(
                    '4645' => 'LG 58',
                    '5765' => 'LG 64'
                  )
      );

$it =  new RecursiveIteratorIterator(new RecursiveArrayIterator($sample));
$l = iterator_to_array($it, false);
echo '<pre>';print_r($l);echo '</pre>';

如果要使用遞歸函數自行實現它:

$sample = array(
        '1232' => 'Nokia 72',
        '234' => array(
            '534' => 'Samsung 58',
            '345' => 'Samsung 64'
            ),
        '3445' => 'Micromax 1542c',
        '542' => array(
            '4645' => 'LG 58',
            '5765' => 'LG 64'
            )
        );

// This recursive function changes the original values!
function walk(&$multi_dim_array, &$flat_array)
{
    while (sizeof($multi_dim_array) > 0)
    {
        // Take the first element
        $curr_value = array_shift($multi_dim_array);
        // If this item is an array go one step deeper
        if (is_array($curr_value))
            walk($curr_value, $flat_array);
        else
            // The current value is not an array
            // append it to the flattened array
            $flat_array[] = $curr_value;
    }
}

$values = [];
walk($sample, $values);
print_r($values);

輸出

Array ( [0] => Nokia 72 [1] => Samsung 58 [2] => Samsung 64 [3] => Micromax 1542c [4] => LG 58 [5] => LG 64 )

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM