简体   繁体   English

我如何排序这个二维数组的PHP

[英]How can I sort this bidimensional array php

I have a conditional that gives my array the value of 2 if its empty otherwise the value will be 1, what I want is to sort this bidimensinal by the value of the associative index for example 我有一个条件,如果数组为空,则将其值设为2,否则值为1,我想要的是例如按关联索引的值对该双精度分类

$arr[$n]["key"] = 1;
$arr[$n]["key"] = 1;
$arr[$n]["key"] = 1;
$arr[$n]["key"] = 2;
$arr[$n]["key"] = 2;

What I want to do is first print the number 1's and then the number 2's 我想做的是先打印数字1,然后打印数字2

I've tried all the methods on this page and nothing works.. please help me. 我已经尝试过此页面上的所有方法,但没有任何效果..请帮助我。 Thanks! 谢谢!

You can just sort your array with asort, quick example: 您可以使用简单的示例对数组进行排序:

<?php

$arr[0][1] = 1;
$arr[0][2] = 1;
$arr[0][3] = 2;
$arr[0][4] = 2;
$arr[0][5] = 1;

asort($arr[0]);

var_dump($arr[0]);

Then you can foreach sort this array with a loop; 然后,您可以使用循环将每个数组排序;

There is a very cool table to choose the function for the excepted result: 有一个非常酷的表可以为例外结果选择函数:

http://php.net/manual/en/array.sorting.php http://php.net/manual/en/array.sorting.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM