Home >Backend Development >PHP Tutorial >数组插入排序

数组插入排序

WBOY
WBOYOriginal
2016-06-06 20:28:541122browse

<code class="php">
$insert = [1=>['name'=>'java','age'=>20],3=>['name'=>'c#','age'=>20]];

$arr = [['name'=>'php','age'=>20],['name'=>'js','age'=>22],['name'=>'python','age'=>23],['name'=>'c','age'=>24]];

//想实现将$insert 按照顺序插入$arr,形成结果:
[['name'=>'php','age'=>20],['name'=>'java','age'=>20],['name'=>'js','age'=>22],['name'=>'c#','age'=>20],['name'=>'python','age'=>23],['name'=>'c','age'=>24]];
//['name'=>'java','age'=>20]排第二 ['name'=>'c#','age'=>20]排第四 $arr原有数据往后排
array_splice($arr,1,0,$insert);//只能实现['name'=>'java','age'=>20]排第二</code>

回复内容:

<code class="php">
$insert = [1=>['name'=>'java','age'=>20],3=>['name'=>'c#','age'=>20]];

$arr = [['name'=>'php','age'=>20],['name'=>'js','age'=>22],['name'=>'python','age'=>23],['name'=>'c','age'=>24]];

//想实现将$insert 按照顺序插入$arr,形成结果:
[['name'=>'php','age'=>20],['name'=>'java','age'=>20],['name'=>'js','age'=>22],['name'=>'c#','age'=>20],['name'=>'python','age'=>23],['name'=>'c','age'=>24]];
//['name'=>'java','age'=>20]排第二 ['name'=>'c#','age'=>20]排第四 $arr原有数据往后排
array_splice($arr,1,0,$insert);//只能实现['name'=>'java','age'=>20]排第二</code>

usort....

快排 usort...

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