基于不同格式数字的多维数组排序

Array

(

    [0] => Array

        (

            [hashtag] => a7e87329b5eab8578f4f1098a152d6f4

            [title] => Flower

            [order] => 3-1-9

        )


    [1] => Array

        (

            [hashtag] => b24ce0cd392a5b0b8dedc66c25213594

            [title] => Free

            [order] => 2-7-5

        )


    [2] => Array

        (

            [hashtag] => e7d31fc0602fb2ede144d18cdffd816b

            [title] => Ready

            [order] => 2-6-3

        )

    [3] => Array

        (

            [hashtag] => e7d31fc0602fb2ede144d18cdffd816b

            [title] => Ready

            [order] => 3-6-3

        )

)

我需要根据“顺序”按降序排序


浮云间
浏览 95回答 3
3回答

白衣非少年

使用 PHP 7 Spaceship Operator <=> 爆炸后使用 usort() 和数组比较:usort($multi, function($a,$b){&nbsp; return explode("-",$b['order']) <=> explode("-",$a['order']);});如果 order 始终具有固定格式,例如 3-1-9(而不是 31-4-345),则可以省略 explode:usort($multi, function($a,$b){return $b['order'] <=> $a['order'];});&nbsp; &nbsp;

慕婉清6462132

你可以试试下面的代码$multi&nbsp; = array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'hashtag'=>'a7e87329b5eab8578f4f1098a152d6f4',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'title'=>'Flower',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'order'=>'3-1-9'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'hashtag'=>'b24ce0cd392a5b0b8dedc66c25213594',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'title'=>'Free',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'order'=>'2-7-5'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'hashtag'=>'e7d31fc0602fb2ede144d18cdffd816b',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'title'=>'Ready',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'order'=>'2-6-3'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'hashtag'=>'e7d31fc0602fb2ede144d18cdffd816b',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'title'=>'Ready',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'order'=>'3-6-3'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ),&nbsp; &nbsp; &nbsp; &nbsp; );function sortByOrder($a, $b) {&nbsp;&nbsp;&nbsp; &nbsp; return str_replace("-","", $a['order']) -&nbsp; str_replace("-","",$b['order']);&nbsp; &nbsp; //For decending use the below code&nbsp; &nbsp; //return str_replace("-","", $b['order']) -&nbsp; str_replace("-","",$a['order']);}usort($multi, 'sortByOrder');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;echo '<pre>';print_r($multi);

侃侃尔雅

您可以对这些值使用explode function,作为分隔符传递-,因此您将从array破折号分隔的数字中获得一个数字字符串。在进行比较之前将数字字符串转换为数字,然后对其进行排序,例如:function cmp($a, $b) {&nbsp; &nbsp; $aArray = explode("-", $a["order"]);&nbsp; &nbsp; $bArray = explode("-", $b["order"]);&nbsp; &nbsp; for ($index = 0; $index < min([count[$aArray], count[$bArray]]); $index++) {&nbsp; &nbsp; &nbsp; &nbsp; $intA = (int)$aArray[$index];&nbsp; &nbsp; &nbsp; &nbsp; $intB = (int)$bArray[$index];&nbsp; &nbsp; &nbsp; &nbsp; if ($intA !== $intB) return (($intA < $intB) ? 1 : -1); //Descending&nbsp; &nbsp; }&nbsp; &nbsp; if (count($aArray) === count($bArray)) return 0;&nbsp; &nbsp; return ((count($aArray) < count($bArray)) ? 1 : -1); //Descending}然后使用这个:usort($yourArray, "cmp");
打开App,查看更多内容
随时随地看视频慕课网APP