排序一个php数组,返回新数组 [英] Sort a php array returning new array

查看:294
本文介绍了排序一个php数组,返回新数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种可靠的标准方法来对数组进行排序,将排序后的(关联的)数组作为返回值返回.

I am looking for a reliable standard method to sort an array, returning the sorted ( associative ) array, as return value.

我已阅读的所有PHP.net函数都返回BOOLEAN值,即0-1. 我需要的方法是这样的:

All the PHP.net functions I have read about return BOOLEAN value, or 0-1. The method I need would be something like:

$some_mixed_array = array( 998, 6, 430 );
function custom_sort( $array )
{ 
  // Sort it
  // return sorted array
}

custom_sort( $some_mixed_array );

// returning: array( 6, 430, 998 )

无需处理字符串,只需处理INT-s.

No need to handle strings, just INT-s.

推荐答案

您能做到这一点吗?

$some_mixed_array = array( 998, 6, 430 );
function custom_sort( $array )
{
  // Sort it
  asort($array);

  // return sorted array
  return $array;
}

custom_sort( $some_mixed_array );

// returning: array( 6, 430, 998 )

这也可以解决您的问题:

This would also solve your issue:

$some_mixed_array = array( 998, 6, 430 );
echo '<pre>'.print_r($some_mixed_array, true).'</pre>';

asort($some_mixed_array); // <- BAM!

// returning: array( 6, 430, 998 )
echo '<pre>'.print_r($some_mixed_array, true).'</pre>';

这篇关于排序一个php数组,返回新数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆