使用多个键合并数组(php)

时间:2015-04-30 15:12:14

标签: php arrays

下面的数组是我目前的情况。通过循环添加新数据。 我试过'array_merge_recursive'以及这个this accepted answer。但它似乎没有用,或者我用错了。

String s = "\n\t\t\t\t\t\t\t2008 Honda Odyssey - 5dr Wgn EX-L\n\t\t\t\t\t\t" //gettext... how ever u get your string

s=s.replaceAll("\n","").replaceAll("\t","");

期望的情况是这样的:

Array (
    [0] => Array (
            [Customer] => Array (
                    [Weekend] => Array (
                            [2016] => Array (
                                    [01] => Array (
                                            [0] => Array (
                                                    [id] => 54
                                                    [startDate] => 01-01-2016
                                                    [endDate] => 31-12-2016
                                                    [price] => 0
                                                )
                                        )
                                )
                        )
                )
        )
    [1] => Array (
            [Customer] => Array (
                    [Weekend] => Array (
                            [2018] => Array (
                                    [01] => Array (
                                            [0] => Array (
                                                    [id] => 56
                                                    [startDate] => 01-01-2018
                                                    [endDate] => 31-12-2018
                                                    [price] => 0
                                                )
                                        )
                                )
                        )
                )
        )
    [2] => Array (
            [Customer] => Array (
                    [Weekend] => Array (
                            [2019] => Array (
                                    [01] => Array (
                                            [0] => Array (
                                                    [id] => 57
                                                    [startDate] => 01-01-2019
                                                    [endDate] => 31-12-2019
                                                    [price] => 0
                                                )
                                        )
                                )
                        )
                )
        )
)

如果需要任何其他信息,请询问!新来的

2 个答案:

答案 0 :(得分:5)

非常确定这会奏效:

$result = call_user_func_array('array_merge_recursive', $array);

答案 1 :(得分:1)

这应该做你想要的:

$new_arr = [];
$arr = // Your current array;

foreach ($arr as $customer)
{
    foreach ($customer['Weekend'] as $year => $z)
    {
        foreach($z as $month => $y)
        {
            foreach($y as $entry)
            {
                Store($year, $month, $entry, $new_arr);
            }
        }
    }

}

function Store($year, $month, $entry, & $new_arr)
{
    if ( ! isset($new_arr['customer'][$year]))
    {
        $new_arr['customer'][$year] = array();
    }
    if ( ! isset($new_arr['customer'][$year][$month]))
    {
        $new_arr['customer'][$year][$month] = array();
    }
    $new_arr['customer'][$year][$month][] = $entry;
}