【发布时间】:2020-05-30 05:56:29
【问题描述】:
我有一个关联数组:
Array (
[0] => Array ( [term_title] => black )
[1] => Array ( [color_quantity] => 2 )
[2] => Array ( [color_price] => 22 )
[3] => Array ( [term_title] => blue )
[4] => Array ( [color_quantity] => 3 )
[5] => Array ( [color_price] => 33 ))
如何改成:
Array (
[0] => Array ( [term_title] => black, [color_quantity] => 2, [color_price] => 22 )
[1] => Array ( [term_title] => blue, [color_quantity] => 3, [color_price] => 33 ) )
我尝试以下代码:
$post = array();
for($i = 0; $i < 2; $i++){
foreach($feild_data as $data){
$post[$i][$data['name']] = $data['value'];
}
}
但它会重复最后一个索引两次。即
Array (
[0] => Array ( [term_title] => blue, [color_quantity] => 3, [color_price] => 33 )
[1] => Array ( [term_title] => blue, [color_quantity] => 3, [color_price] => 33 ) )
【问题讨论】:
-
这个变化背后的算法是什么?
-
您的代码引用了输出中没有的一些内容 -
$post['item_' . $i]、$data['name']和$data['value'],因此不确定您是如何实现输出的。 -
我编辑 $post['item_' 。 $i] 到 $post[$i],它的一般。 $data 又是一个数组,其中包含键和值作为名称和值。此数组来自表单中的 jQuery serializedArray
-
@mickmackusa,不确定骗子是否真的适合,在这种情况下,他们没有使用关联数组,因此会丢失所有键(我认为)。
标签: php arrays multidimensional-array grouping