多维数组与子项连接并回显

时间:2015-04-09 13:46:22

标签: php arrays codeigniter csv multidimensional-array

我有以下格式:

Array
(
[id] => 325
[type] => 
[p_type] => 
[fees] => 
[sku] => 412
[amountt] => 
)
Array
(
[id] => 325
[type] => 
[p_type] => Initial
[fees] => 
[sku] => 412
[amount] => 2.22)

Array
(
[id] => 325
[type] => 
[p_type] => Shipping
[fees] => 
[sku] => 412
[amount] =>0.22
)

Array
(
[id] => 325
[type] => shipping_add
[p_type] => 
[fees] => 
[sku] => 412
[amount] => 0.50 )

Array
(
[id] => 325
[type] => comms
[p_type] => 
[fees] => 
[sku] => 412
[amount] => 1.22
)

我可以通过说:

获取每个值并显示回来
$arr[id] = $idCars;

并且很容易在我的主题中显示,但是我想要做的是实际合并数据 - 所以我只有一个数据条目用于该ID,带有子 - >子数组,任何想法?< / p>

请求的数组格式:

[0] => 325 412 
[1] => 325 412 Initial 2.22 
[2] => 325 412 comms -1.22 
[3] => 325 412 Shipping -0.22 
[4] => 325 412 Shipping_add -0.50 
[5] => 326 444 Shipping -1.45 
[6] => 326 444 Initial 0.91

我通过以下方式重新格式化:

$result = array();
$result[id] = $id;
$result[type] =  $type;
$result[p_type] =  $ptype;
$result[fees] = $fees;
$result[sku] = $sku;
$result[amount] =  $amount;

依旧......

1 个答案:

答案 0 :(得分:0)

不确定你要做什么,但我会试一试。如果您希望子数组是无密钥的,PHP中的所有数组都有密钥。请参阅:Creating an array without keys in a loop

如果您仍然坚持要求输出采用建议的格式,则可以将每个子数组存储为如下字符串:

$new_arr = array();
$count = 0;

foreach ($arr as $val) {

    $new_arr[$count] = $val['id'].' '.$val['type'].' '.$val['p_type'].' '.$val['fees'].' '.$val['sku'].' '.$val['amount'];
    $count++;

}

$arr = $new_arr;