У меня многомерный массив
[0] => Array ( [name] => Size [value] => XS,S,XL ) [1] => Array ( [name] => Brand [value] => Adidas ) [2] => Array ( [name] => Size [value] => XS,XL,L,M ) [3] => Array ( [name] => Brand [value] => Nike )
я хочу результат как
[0] => Array ( [name] => Size [value] => S,M,L,XS,XL ) [1] => Array ( [name] => Brand [value] => Adidas,Nike )
Я пытаюсь array_combine
, array_merge
и даже array_unique
без успеха.
Вы можете попробовать это –
$array = array( '0' => array ( 'name' => 'Size', 'value' => 'XS,S,XL', ), '1' => array ( 'name' => 'Brand', 'value' => 'Adidas', ), '2' => array ( 'name' => 'Size', 'value' => 'XS,XL,L,M', ), '3' => array ( 'name' => 'Brand', 'value' => 'Nike', ) ); $temp = $new = array(); foreach($array as $val) { $temp[$val['name']][] = $val['value']; } foreach($temp as $key => $value) { // merge values, fetch unique, then merge again $values = implode(',', array_unique(explode(',', implode(',', $value)))); $new[] = array('name' => $key, 'value' => $values); // store } var_dump($new);
Вывод
array(2) { [0]=> array(2) { ["name"]=> string(4) "Size" ["value"]=> string(11) "XS,S,XL,L,M" } [1]=> array(2) { ["name"]=> string(5) "Brand" ["value"]=> string(11) "Adidas,Nike" } }
демонстрация