如何通过"order"键的值对此数组进行排序?尽管这些值目前是连续的,但它们并不总是如此.
Array
(
[0] => Array
(
[hashtag] => a7e87329b5eab8578f4f1098a152d6f4
[title] => Flower
[order] => 3
)
[1] => Array
(
[hashtag] => b24ce0cd392a5b0b8dedc66c25213594
[title] => Free
[order] => 2
)
[2] => Array
(
[hashtag] => e7d31fc0602fb2ede144d18cdffd816b
[title] => Ready
[order] => 1
)
)
Run Code Online (Sandbox Code Playgroud) 如何通过其中一个字段对这个对象数组进行排序,比如name或count?
Array
(
[0] => stdClass Object
(
[ID] => 1
[name] => Mary Jane
[count] => 420
)
[1] => stdClass Object
(
[ID] => 2
[name] => Johnny
[count] => 234
)
[2] => stdClass Object
(
[ID] => 3
[name] => Kathy
[count] => 4354
)
....
Run Code Online (Sandbox Code Playgroud) 我有一个像这样的阵列
Array (
[0] => Array( "destination" => "Sydney",
"airlines" => "airline_1",
"one_way_fare" => 100,
"return_fare => 300
),
[2] => Array( "destination" => "Sydney",
"airlines" => "airline_2",
"one_way_fare" => 150,
"return_fare => 350
),
[3] => Array( "destination" => "Sydney",
"airlines" => "airline_3",
"one_way_fare" => 180,
"return_fare => 380
)
)
我如何通过return_fare asc,one_way_fare asc对值进行排序?
我试过array_multisort()但我最终得到了混合数据..
asort仅适用于一维数组,我需要按两个值或更多值排序,我如何在SQL中实现这一点,按field1 asc,field2 asc排序?
所以我们在PHP中使用了这个函数
strcmp(string $1,string $2) // returns -1,0, or 1;
Run Code Online (Sandbox Code Playgroud)
但是,我们不要有一个intcmp(); 所以我创建了一个:
function intcmp($a,$b) {
if((int)$a == (int)$b)return 0;
if((int)$a > (int)$b)return 1;
if((int)$a < (int)$b)return -1;
}
Run Code Online (Sandbox Code Playgroud)
这只是感觉很脏.你们都觉得怎么样?
这是通过传入的排序值对Javascripts进行排序的类的一部分.
class JS
{
// array('order'=>0,'path'=>'/js/somefile.js','attr'=>array());
public $javascripts = array();
...
public function __toString()
{
uasort($this->javascripts,array($this,'sortScripts'));
return $this->render();
}
private function sortScripts($a,$b)
{
if((int)$a['order'] == (int)$b['order']) return 0;
if((int)$a['order'] > (int)$b['order']) return 1;
if((int)$a['order'] < (int)$b['order']) return -1;
}
....
}
Run Code Online (Sandbox Code Playgroud) 是否可以用于usort对多维数组中的多个字段进行排序?例如,我想name按字母顺序排序,然后从那些我想要排序的记录中排序age.这可能用sort吗?
Array (
[0] => Array (
[name] => Jonah
[age] => 27
)
[1] => Array (
[name] => Bianca
[age] => 32
)
)
Run Code Online (Sandbox Code Playgroud) 我有一个数组
Array
(
[0] => stdClass Object
(
[tab_option_name_selector] => 2
[fieldtype] => notes
[order] => 12
)
[1] => stdClass Object
(
[tab_option_name_selector] => 2
[fieldtype] => notes
[order] => 8
)
[2] => stdClass Object
(
[tab_option_name_selector] => 1
[order] => 2
[fieldtype] => selectbox
)
[3] => stdClass Object
(
[tab_option_name_selector] => 2
[order] => 3
[fieldtype] => selectbox
)
)
Run Code Online (Sandbox Code Playgroud)
我正在尝试让这个 usort 函数工作
function osort(&$array, $props)
{
if(!is_array($props))
$props = array($props => true);
$me = usort($array, …Run Code Online (Sandbox Code Playgroud) 我目前正在创建一个由mysql查询中的值组成的排序方法.
这是阵列的简要视图:
Array
(
[0] => Array
(
['id'] = 1;
['countries'] = 'EN,CH,SP';
)
[1] => Array
(
['id'] = 2;
['countries'] = 'GE,SP,SV';
)
)
Run Code Online (Sandbox Code Playgroud)
我已经成功地根据数字id值进行了正常的操作,但是我更愿意按"countries"字段的内容对数组进行排序(如果它包含一个设置字符串,在这种情况下是一个国家/地区代码),然后由id字段.
以下片段是我对如何做到的第一个想法,但我不知道如何将它合并到一个工作函数中:
in_array('EN', explode(",",$a['countries']) );
Run Code Online (Sandbox Code Playgroud)
你会怎么做?
谢谢!
不幸的是,我真的无处可去.
这就是我现在拥有的东西,它给我的只是错误: uasort() [function.uasort]: Invalid comparison function
function compare($a, $b) {
global $usercountry;
if ( in_array($usercountry, $a['countries']) && in_array($usercountry, $a['countries']) ) {
$return = 0;
}
else if (in_array($usercountry, $a['countries'])) {
$return = 1;
}
else {
$return = -1;
}
return $return;
}
$array= usort($array, …Run Code Online (Sandbox Code Playgroud) 如何按主键和辅助键对多维数组进行排序?例如,假设以下数组:
$result = array();
$result[0]["prio"] = 1;
$result[0]["date"] = '2010-02-28';
$result[0]["post"] = "February's thoughts";
$result[1]["prio"] = 0;
$result[1]["date"] = '2010-04-20';
$result[1]["post"] = "April's thoughts";
$result[2]["prio"] = 0;
$result[2]["date"] = '2010-05-30';
$result[2]["post"] = "May's thoughts";
Run Code Online (Sandbox Code Playgroud)
我希望将列'prio'排序为主键(升序)和'date'作为辅助键(降序),以获得:
$result[0]["prio"] = 0;
$result[0]["date"] = '2010-05-30';
$result[0]["post"] = "May's thoughts";
$result[1]["prio"] = 0;
$result[1]["date"] = '2010-04-20';
$result[1]["post"] = "April's thoughts";
$result[2]["prio"] = 1;
$result[2]["date"] = '2010-02-28';
$result[2]["post"] = "February's thoughts";
Run Code Online (Sandbox Code Playgroud)
怎么做到这一点?
我有一个多维数组,看起来像:
$arr=Array
(
[0] => Array
(
[0] => TEAM1
[1] => 3
[2] => 0
[3] => 422.47
[4] => 192.62
)
[1] => Array
(
[0] => TEAM2
[1] => 2
[2] => 1
[3] => 402.14
[4] => 210.70
)
[2] => Array
(
[0] => TEAM3
[1] => 3
[2] => 0
[3] => 376.79
[4] => 174.64
)
)
Run Code Online (Sandbox Code Playgroud)
这5栏与团队名称,#胜利,#损失,#的得分,#的得分有关。
我将如何$arr按第1列(获胜次数)(降序),第2列(损失数)(升序),然后第3列(要获得的点数)(降序)进行排序
我有一个表/数组。例如,在 Excel 中,我可以按第 1 列 asc、第 2 列 desc、第 3 列 asc 等排序。
我可以在 PHP 中做同样的事情吗?首先是["first_name"] ASC,然后["last_name"] DESC是["player_id"] ASC和["user_id"] DESC。
array(2) {
[0]=> array(6) {
[0]=> string(8) "John",
["first_name"]=> string(8) "John",
[1]=> int(7) "44",
["score"]=> int(7) "44",
[2]=> string(2) "7",
["player_id"]=> string(2) "7",
[3]=> string(2) "3",
["user_id"]=> string(2) "3"
},
[1]=> array(6) {
[0]=> string(5) "Sam",
["first_name"]=> string(5) "Sam",
[1]=> int(7) "55",
["score"]=> int(7) "55",
[2]=> string(2) "1",
["player_id"]=> string(2) "1",
[3]=> …Run Code Online (Sandbox Code Playgroud)