0
我想根據他們的sku按字母順序對數組進行排序,並且我用三個循環編寫了這個程序,我想我沒有把它寫成最好的解決方案,您還有其他更好的解決方案嗎?您對此有何想法?你知道更好的性能解決方案排序數組中的數組?
$array[] = array('data' => array('entity_id' => 3446,'sku' => 'A'));
$array[] = array('data' => array('entity_id' => 3546,'sku' => 'D'));
$array[] = array('data' => array('entity_id' => 7446,'sku' => 'C'));
$array[] = array('data' => array('entity_id' => 2446,'sku' => 'B'));
$array[] = array('data' => array('entity_id' => 7446,'sku' => 'E'));
$array[] = array('data' => array('entity_id' => 9446,'sku' => 'F'));
foreach ($array as $key => $data) {
foreach ($data as $k=> $v)
{
$newarray[$key] = $v['sku'];
}
}
asort($newarray);
foreach ($newarray as $k=>$v)
$keys[]=$k;
$result=array();
foreach($keys as $k=>$v) {
$result[$k] = $array[$v];
}
除了下面的答案,PHP有很多的方法來排序數組。 http://www.php.net/manual/en/array.sorting.php – Adam