Home > Backend Development > PHP Tutorial > PHP custom two-dimensional array sorting

PHP custom two-dimensional array sorting

不言
Release: 2023-03-24 17:58:01
Original
2077 people have browsed it

The main content of this article is about PHP custom two-dimensional array sorting, which has certain reference value. Now I share it with everyone. Friends in need can refer to it

function array_sort($arr,$keys,$type='asc'){
        $keysvalue = $new_array = array();
        foreach ($arr as $k=>$v){
            $keysvalue[$k] = $v[$keys];
        }
        if($type == 'asc'){
            asort($keysvalue);
        }else{
            arsort($keysvalue);
        }
        reset($keysvalue);
        echo "<pre class="brush:php;toolbar:false">";
        print_r($keysvalue);
        foreach ($keysvalue as $k=>$v){
            $new_array[$k] = $arr[$k];
        }
        return $new_array;
    }
Copy after login
<br/>
Copy after login
$array = array(
    array(&#39;name&#39;=>&#39;手机&#39;,&#39;brand&#39;=>&#39;诺基亚&#39;,&#39;price&#39;=>1050),
    array(&#39;name&#39;=>&#39;笔记本电脑&#39;,&#39;brand&#39;=>&#39;lenovo&#39;,&#39;price&#39;=>4300),
    array(&#39;name&#39;=>&#39;剃须刀&#39;,&#39;brand&#39;=>&#39;飞利浦&#39;,&#39;price&#39;=>3100),
    array(&#39;name&#39;=>&#39;跑步机&#39;,&#39;brand&#39;=>&#39;三和松石&#39;,&#39;price&#39;=>4900),
    array(&#39;name&#39;=>&#39;手表&#39;,&#39;brand&#39;=>&#39;卡西欧&#39;,&#39;price&#39;=>960),
    array(&#39;name&#39;=>&#39;液晶电视&#39;,&#39;brand&#39;=>&#39;索尼&#39;,&#39;price&#39;=>6299),
    array(&#39;name&#39;=>&#39;激光打印机&#39;,&#39;brand&#39;=>&#39;惠普&#39;,&#39;price&#39;=>1200)
);
$ShoppingList = array_sort($array,&#39;price&#39;);
echo "<pre class="brush:php;toolbar:false">";
print_r($ShoppingList);
Copy after login

Related recommendations:

PHP two-dimensional array sorted by key value

php two-dimensional array deduplication method

PHP two-dimensional array Sort array_multisort




The above is the detailed content of PHP custom two-dimensional array sorting. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template