将更多数组转换为单个数组

时间:2016-07-25 06:21:42

标签: php

将这些数组转换为一个数组

Array ( [0] => 10 ) Array ( [0] => 17 ) Array ( [0] => 17 ) Array ( [0] => 15 )

我想要这样的输出:

Array ( [0] => 10 ,[1] => 17,[2] => 17,[3] => 15)

3 个答案:

答案 0 :(得分:0)

$a=array(10);$b=array(17);$c=array(17);$d=array(15);

print_r(array_merge($a,$b,$c,$d));

//Array([0]=>10 [1]=>17 [2]=>17 [3]=>15)

答案 1 :(得分:0)

假设您的Main Array包含一些嵌套在其中的子数组,如下所示:
    Bundle b = getIntent().getExtras();
    String message = b.getString("key", ""); // the blank String in the second parameter is the default value of this variable. In case the value from previous activity fails to be obtained, the app won't crash: instead, it'll go with the default value of an empty string
现在;您想要取出子阵列中每个元素的所有值,并将它们转换为主阵列的直接元素。您可以使用array_walk ...然后构建具有您预期的结构的Flat Array,如下所示:
<?php
    $arrNestedArray = array(
        array(10),
        array(17),
        array(17),
        array(15),
        array("data"=>array("fName"=>"Cosmic", "lName"=>"Joy")),
    );
最后,你可能仍然希望test it out here;上面的var_dump产生类似这样的东西:
<?php
    // CREATE AN EMPTY ARRAY TO HOLD THE FINAL RESULT YOU DESIRED...
    $singleArray    = array();

    // THIS IS A SAMPLE OF THE MAIN ARRAY CONTAINING SUB ARRAYS...
    $arrNestedArray = array(
        array(10),
        array(17),
        array(17),
        array(15),
        array("data"=>array("fName"=>"Cosmic", "lName"=>"Joy")),
    );

    array_walk($arrNestedArray, function($data, $index) use(&$singleArray) {
        if( is_array($data) ) {
            foreach ($data as $key=>$item) {
                if(!in_array($item, $singleArray)) {
                    if(is_array($item)) {
                        $singleArray[$key]   = $item;
                    }else{
                        $singleArray[$index] = $item;
                    }
                }
            }
        }
    });
    var_dump($singleArray);

答案 2 :(得分:-1)

array_mergecall_user_func_array

一起使用
$a = array(Array ( 0 => 10 ), Array ( 0 => 17 ), Array ( 0 => 17 ), Array ( 0 => 15 ));
$ra = call_user_func_array('array_merge', $a);
print_r($ra); // Array ( [0] => 10 [1] => 17 [2] => 17 [3] => 15 )