0
如果關聯數組包含整數鍵,則array_multisort
會自動重置它們,所以我正在丟失數據。這是一個小例子。任何解決方案使用PHP array_multisort函數對多維關聯數組進行排序,其中包含一些整數鍵
$products = array ('777.777' => array('price' => 10, 'name' => 'a'),
'777' => array('price' => 100, 'name' => 'b')
);
$sort_field = 'price';
$destination = SORT_ASC;
$sort_array = array();
foreach ($products as $productId => $product) {
$sort_array[$productId] = $product[$sort_field];
}
array_multisort($sort_array, $destination, $products);
print_r($products);
// Array ([777.777] => Array ([price] => 10 [name] => a) [0] => Array ([price] => 100 [name] => b))