php Natsort的例子

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php Natsort的例子相关的知识,希望对你有一定的参考价值。

<span style="color: #808080; font-style: italic;">// create a example array</span>
<span style="color: #0000ff;">$array_1</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span>
    <span style="color: #ff0000;">'pic10.jpg'</span>, <span style="color: #ff0000;">'pic3.jpg'</span>, <span style="color: #ff0000;">'pic1.jpg'</span>, 
    <span style="color: #ff0000;">'pic7.jpg'</span>, <span style="color: #ff0000;">'pic99.jpg'</span>, <span style="color: #ff0000;">'pic127.jpg'</span>
<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// copy array 1</span>
<span style="color: #0000ff;">$array_2</span> = <span style="color: #0000ff;">$array_1</span>;
&nbsp;
&nbsp;
<a href="http://www.php.net/sort"><span style="color: #000066;">sort</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$array_1</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;Standard sorting algorithm:&lt;br/&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
<a href="http://www.php.net/print_r"><span style="color: #000066;">print_r</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$array_1</span><span style="color: #66cc66;">&#41;</span>;
<a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
&nbsp;
&nbsp;
<a href="http://www.php.net/natsort"><span style="color: #000066;">natsort</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$array_2</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<a href="http://www.php.net/print"><span style="color: #000066;">print</span></a> <span style="color: #ff0000;">&quot;Natural order sorting:&lt;br/&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
<a href="http://www.php.net/print_r"><span style="color: #000066;">print_r</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$array_2</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">/*
Output:
&nbsp;
Standard sorting algorithm
Array
(
    [0] =&gt; pic1.jpg
    [1] =&gt; pic10.jpg
    [2] =&gt; pic127.jpg
    [3] =&gt; pic3.jpg
    [4] =&gt; pic7.jpg
    [5] =&gt; pic99.jpg
)
&nbsp;
&nbsp;
Natural order sorting
Array
(
    [2] =&gt; pic1.jpg
    [1] =&gt; pic3.jpg
    [3] =&gt; pic7.jpg
    [0] =&gt; pic10.jpg
    [4] =&gt; pic99.jpg
    [5] =&gt; pic127.jpg
)
*/</span>

以上是关于php Natsort的例子的主要内容,如果未能解决你的问题,请参考以下文章

Python自然排序(natsort)

PHP版本对比

PHP 数组排序相关函数总结

二维数组重新排序

自然排序 Pandas DataFrame

PHP正则匹配与替换的简单例子