Home >Backend Development >PHP Tutorial >PHP sort two-dimensional array by key value

PHP sort two-dimensional array by key value

WBOY
WBOYOriginal
2016-07-29 09:04:281048browse

Slow and steady - the first technical blog post

<code><span>/**
     * 按二维数组的键值排序
     *<span> @param</span> unknown $array 二维数组
     *<span> @param</span> unknown $key   二维数组的键值
     *<span> @param</span> string $order  升序/降序
     *<span> @return</span> multitype:unknown  返回排序后的数组
     */</span><span>public</span><span>static</span><span><span>function</span><span>arr_sort_in_key</span><span>(<span>$array</span>, <span>$key</span>, <span>$order</span> = <span>"asc"</span>)</span>
    {</span><span>$arr_nums</span> = <span>$arr</span> = <span>array</span>();

        <span>foreach</span> (<span>$array</span><span>as</span><span>$k</span> => <span>$v</span>)
        {
            <span>$arr_nums</span>[<span>$k</span>] = <span>$v</span>[<span>$key</span>];
        }

        <span>if</span>(<span>$order</span>==<span>'asc'</span>)
        {
            asort(<span>$arr_nums</span>);
        }
        <span>else</span>
        {
            arsort(<span>$arr_nums</span>);
        }

        <span>foreach</span>(<span>$arr_nums</span><span>as</span><span>$k</span>=><span>$v</span>)
        {
            <span>$arr</span>[] = <span>$array</span>[<span>$k</span>];
        }

        <span>return</span><span>$arr</span>;

    }
</code>
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

The above introduces the PHP sorting by key value of the two-dimensional array, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
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