如何重构数组两个索引数组[重复]

问题描述 投票:-1回答:1

我有一个关联数组:

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 ) )
php arrays multidimensional-array
1个回答
0
投票

不确定namevalue$data['name']$data['value']中来自何处,但是您可以使用array_chunk将它们分成多个块并将它们分配给array_chunk

$posts
© www.soinside.com 2019 - 2024. All rights reserved.