【发布时间】:2014-12-29 01:52:04
【问题描述】:
我试图将两个或多个数组中的一些数字添加到一个数组中。我的问题是,它总是添加另一个索引。
源数组如下所示:
array(
(int) 0 => array(
'Sale' => array(
'id' => '1',
'market_id' => '1',
'product_ids' => '1,2,3,4,5,6,7,8',
'date_and_time' => '2014-12-28 00:00:00',
'money_spent' => '2344',
'points_given' => '213'
)
),
(int) 1 => array(
'Sale' => array(
'id' => '2',
'market_id' => '1',
'product_ids' => '44,3,32,23,12,32',
'date_and_time' => '2014-12-28 15:25:38',
'money_spent' => '123',
'points_given' => '2'
)
)
)
我用来合并数组和分解 product_ids 字段中的数字的 PHP 代码
$sales=array();
foreach ($sales_detailed as $sale_detailed): {
$sale_detailed_ids=explode( ',', $sale_detailed['Sale']['product_ids'] );
array_push($sales, $sale_detailed_ids);
} endforeach;
结果是
array(
(int) 0 => array(
(int) 0 => '1',
(int) 1 => '2',
(int) 2 => '3',
(int) 3 => '4',
(int) 4 => '5',
(int) 5 => '6',
(int) 6 => '7',
(int) 7 => '8'
),
(int) 1 => array(
(int) 0 => '44',
(int) 1 => '3',
(int) 2 => '32',
(int) 3 => '23',
(int) 4 => '12',
(int) 5 => '32'
)
)
虽然我希望它看起来像这样
array(
(int) 0 => array(
(int) 0 => '1',
(int) 1 => '2',
(int) 2 => '3',
(int) 3 => '4',
(int) 4 => '5',
(int) 5 => '6',
(int) 6 => '7',
(int) 7 => '8'
(int) 8 => '44',
(int) 9 => '3',
(int) 10 => '32',
(int) 11 => '23',
(int) 12 => '12',
(int) 13 => '32'
)
)
【问题讨论】:
-
"array_merge" 对于数字索引与字符串索引的行为不同。阅读文档。如果您想在某个级别保持数字索引相同,则必须编写一些自定义代码。