简体   繁体   English

如何在php中对数组进行排序?

[英]how can I sort an array in php?

Example : 范例:

$a [] = array ('google','bing','yahoo');
$a [] = array ('America','France','Germany');
$a [] = array ('Africa','Asia','Europe');

And I want to sort her in table like that : 我想像这样在表中对她进行排序:

Africa....... America........ google....... 非洲.......美国........谷歌.......

You can do sort($a) . 您可以执行sort($a)

This first Google result for "sort array php" is this: 这是“ sort array php”的第一个Google搜索结果:

http://php.net/manual/en/function.sort.php http://php.net/manual/en/function.sort.php

You can use array_multisort , specially if your inner arrays has same size. 您可以使用array_multisort ,特别是如果内部数组的大小相同。 I have checcked it, hope it will serve as you need. 我已选中它,希望它能按您的需要使用。

  1. Why your array is multidimensional? 为什么数组是多维的?
  2. Join your nested arrays into one, sort it, and then you can output that sorted array in a manner you wish 将嵌套的数组合并为一个,对其进行排序,然后可以按照希望的方式输出排序后的数组

Try this: 尝试这个:

$resort = "return (1*strnatcmp(\$x['0'], \$y['0']));";
uasort($a, create_function('$x,$y', $resort));
print_r($a);

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

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