PHP array_multisort 抛出错误
PHP array_multisort throws error
我是 PHP 的新手,正在尝试对关联数组列表进行排序。我想在一个家庭字段上对它们进行排序,只是一个字符串,按降序排列。每当我尝试对关联数组...数组进行排序时,它都会给我这个错误。
Warning: array_multisort(): Argument #1 is expected to be an array or a sort flag.
PHP代码
<?php
$allColors = array(
'0' => array(
"id"=> "1",
"family"=> "blue",
"name"=> "ariel blue",
"hex"=> "#339FFF"),
'3' => array(
"id"=> "3",
"family"=> "green",
"name"=> "forest",
"hex"=> "#FAFF33"),
'1' => array(
"id"=> "2",
"family"=> "blue",
"name"=> "aqua marine",
"hex"=> "#339FFF"),
'4' => array(
"id"=> "4",
"family"=> "green",
"name"=> "lime",
"hex"=> "#FAFF33"),
'2' => array(
"id"=> "5",
"family"=> "blue",
"name"=> "teal",
"hex"=> "#339FFF"),
'5' => array(
"id"=> "6",
"family"=> "green",
"name"=> "yellow-green",
"hex"=> "#FAFF33")
);
array_multisort($family, SORT_DESC, $allColors);
?>
如果您尝试按 "family" 对数组进行排序,则 array_multisort 的第一个参数需要是 "family" 列的内容。
array_multisort( array_column( $allColors, 'family' ), SORT_DESC, $allColors );
我是 PHP 的新手,正在尝试对关联数组列表进行排序。我想在一个家庭字段上对它们进行排序,只是一个字符串,按降序排列。每当我尝试对关联数组...数组进行排序时,它都会给我这个错误。
Warning: array_multisort(): Argument #1 is expected to be an array or a sort flag.
PHP代码
<?php
$allColors = array(
'0' => array(
"id"=> "1",
"family"=> "blue",
"name"=> "ariel blue",
"hex"=> "#339FFF"),
'3' => array(
"id"=> "3",
"family"=> "green",
"name"=> "forest",
"hex"=> "#FAFF33"),
'1' => array(
"id"=> "2",
"family"=> "blue",
"name"=> "aqua marine",
"hex"=> "#339FFF"),
'4' => array(
"id"=> "4",
"family"=> "green",
"name"=> "lime",
"hex"=> "#FAFF33"),
'2' => array(
"id"=> "5",
"family"=> "blue",
"name"=> "teal",
"hex"=> "#339FFF"),
'5' => array(
"id"=> "6",
"family"=> "green",
"name"=> "yellow-green",
"hex"=> "#FAFF33")
);
array_multisort($family, SORT_DESC, $allColors);
?>
如果您尝试按 "family" 对数组进行排序,则 array_multisort 的第一个参数需要是 "family" 列的内容。
array_multisort( array_column( $allColors, 'family' ), SORT_DESC, $allColors );