sort array in php, merging two values together
I have the following array from a while($x = mysql_fetch_assoc...):
Array ( [item_开发者_StackOverflow中文版id] => 1 [item_name] => name [foo] => bar )
How would I modify the array so it returns me:
Array ( [item_id] => 1 [item_name] => name - bar )
$array['item_name'] = "{$array['item_name']} - {$array['foo']}";
unset($array['foo']);
or alternatively
$array = array(
'item_id' => $array['item_id'],
'item_name' => "{$array['item_name']} - {$array['foo']}"
);
If you always want to merge [item_name] and [foo] values, then it's pretty easy:
$result = array_map(function($item) {
return array(
'item_id' => $item['item_id'],
'item_name' => $item['item_name'] . ' - ' . $item['foo'],
);
}, $input);
精彩评论