将数组推入另一个数组PHP

时间:2014-07-23 13:41:44

标签: php

我在循环中创建了多个数组。单个数组如下所示:

'Event' => array(
        'customer_id' => '107',
        'user_id' => '124',
        'date' => '23-07-2014',
        'time-options' => '0',
    )

然后我想将数组推入另一个数组,预期结果必须如下:

$finalData = array(
    array('Event' => array('customer_id' => '107','user_id' => '124','date' => '23-07-2014','time-options' => '0',)),
    array('Event' => array('customer_id' => '108'...)),
);

我一直在尝试array_push(),但它给了我一个索引数组,这不是预期的结果。

我期待着任何形式的帮助或指导。

1 个答案:

答案 0 :(得分:3)

试试这个:

$finalData = array();

然后只需添加如下数组:

$finalData[] = array(
    'Event' => array(
        'customer_id' => '107',
        'user_id' => '124',
        'date' => '23-07-2014',
        'time-options' => '0',
    )
);
$finalData[] = array(
    'Event' => array(
        'customer_id' => '123',
        'user_id' => '123',
        'date' => '23-07-2014',
        'time-options' => '1',
    )
);

请注意,$ finalData也是一个索引数组,当你说你期待这个时:

$finalData = array(
    array('Event' => array('customer_id' => '107','user_id' => '124','date' => '23-07-2014','time-options' => '0',)),
    array('Event' => array('customer_id' => '108'...)),
);

它与:

相同
$finalData = array(
    0 => array('Event' => array('customer_id' => '107','user_id' => '124','date' => '23-07-2014','time-options' => '0',)),
    1 => array('Event' => array('customer_id' => '108'...)),
);