分层数组转换为平面数组

时间:2016-06-10 19:43:31

标签: php

我有一个数组

$array = array(
    array(1,2,3),
    array('bob','bill'),
    array(4,5,6,7) 
    //can have between 0 and 5 "rows"
);

我想转换为一个看起来像这样的数组......

[1,bob,4, 1, bob, 5, 1, bob, 6, 1, bob, 7, 1, bill, 4 .... 3, bill, 7]

我试图为此编写一个递归函数,看起来像......

function GetValues($array, $rowIndex, $valIndex, $values)
{
    if(!isset($array[$rowIndex]))
    {
        return $values;
    }
    if(!isset($array[$rowIndex][$valIndex]))
    {
        return GetValues($array,$rowIndex+1, 0, $values);
    }
    $values[] = $array[$rowIndex][$valIndex];

    return  GetValues($array,$rowIndex, $valIndex+1, $values);
}

它最终会正常迭代数组(打印1,2,3,bob,bill,4,5,6,7)

2 个答案:

答案 0 :(得分:2)

$array = array(
    array(1,2,3),
    array('bob','bill'),
    array(4,5,6,7) 
);
function combinations($arrays) {
    $result[] = array();
    foreach ($arrays as $key => $values) {
        $tmp = array();
        foreach ($result as $result_item) {
            foreach ($values as $value) {
                $tmp[] = array_merge($result_item, array($key => $value));
            }
        }
        $result = $tmp;
    }

    return $result;
}
$combinations_array = combinations($array);
$flat_string = '';
$flat_array = array();
foreach($combinations_array as $value){
        foreach($value as $v){
            $flat_string.=$v.',';
            $flat_array[]=$v;
        }
    }

echo rtrim($flat_string,',');
print_r($flat_array);

输出字符串:      1,鲍勃,4,1,鲍勃,5,1,鲍勃,6,1,鲍勃,7,1,票据,4,1,票据,5,1,票据,6,1,票据,7,2,鲍勃,4,2,鲍勃,5,2,鲍勃,6,2,鲍勃,7,2,票据,4,2,票据,5,2,票据,6,2,票据,7,3,鲍勃, 4,3,鲍勃,5,3,鲍勃,6,3,鲍勃,7,3,票据,4,3,票据,5,3,票据,6,3,票据,7

输出数组:

    Array
(
    [0] => 1
    [1] => bob
    [2] => 4
    [3] => 1
    [4] => bob
    [5] => 5
    [6] => 1
    [7] => bob
    [8] => 6
    [9] => 1
    [10] => bob
    [11] => 7
    [12] => 1
    [13] => bill
    [14] => 4
    [15] => 1
    [16] => bill
    [17] => 5
    [18] => 1
    [19] => bill
    [20] => 6
    [21] => 1
    [22] => bill
    [23] => 7
    [24] => 2
    [25] => bob
    [26] => 4
    [27] => 2
    [28] => bob
    [29] => 5
    [30] => 2
    [31] => bob
    [32] => 6
    [33] => 2
    [34] => bob
    [35] => 7
    [36] => 2
    [37] => bill
    [38] => 4
    [39] => 2
    [40] => bill
    [41] => 5
    [42] => 2
    [43] => bill
    [44] => 6
    [45] => 2
    [46] => bill
    [47] => 7
    [48] => 3
    [49] => bob
    [50] => 4
    [51] => 3
    [52] => bob
    [53] => 5
    [54] => 3
    [55] => bob
    [56] => 6
    [57] => 3
    [58] => bob
    [59] => 7
    [60] => 3
    [61] => bill
    [62] => 4
    [63] => 3
    [64] => bill
    [65] => 5
    [66] => 3
    [67] => bill
    [68] => 6
    [69] => 3
    [70] => bill
    [71] => 7
)

答案 1 :(得分:0)

您可能遇到的问题是不知道原始阵列有多大。您可以通过销毁数组来递归写入它。在这个例子中,我将第一个元素从原始数组移开。然后,每次递归调用,我都会移出另一个元素。我使用for循环来遍历值并返回我已构建的内容。

function flatten($array)
{
    if(sizeof($array)==0) return null;
    $first = array_shift($array);
    $ret = array();
    foreach($first as $value)
    {
        $ret[] = $value;
        $sub = flatten($array);
        if($sub != null)
            $ret = array_merge($ret, $sub);
    }
    return $ret;
}