Rumah > Soal Jawab > teks badan
Masalah ini agak serupa dengan pesanan oleh dalam Mysql Apa yang diperlukan adalah untuk mensimulasikan pengisihan medan yang berbeza dalam tatasusunan.
Andaikan terdapat tatasusunan berikut:
$beforeSort = [
"0" => ["nama" => "Zhang San", "bahasa Inggeris" =>
"1" => ["nama" => "李思", "bahasa Inggeris" =>
"2" => ["nama" => "老王", "bahasa Inggeris" =>
];
Sekarang anda perlu mengikut susunan cina
dalam tatasusunan Jika ia adalah sama, ikut susunan math
Hasil akhir mestilah susunan berikut:< /p>
$afterSort = [
"2" => ["nama" => "老王", "bahasa Inggeris" =>
"0" => ["nama" => "Zhang San", "bahasa Inggeris" =>
"1" => ["nama" => "李思", "bahasa Inggeris" =>
];
Adakah anda mempunyai cara berbeza untuk mencapai ini?
ringa_lee2017-05-16 13:10:20
Ini adalah versi yang saya gunakan sendiri Cara menggunakannya:
$afterSort = getArraySort($beforeSort, 'chinese', 'SORT_ASC', 'math', 'SORT_ASC');
/**
* 二维数组排序(数字索引数组将重建索引)
* @param array $arr 需要排序的数组 二维数组
* @param string $arg1 排序的键名或字段名
* @param string $arg2 排序的顺序 SORT_ASC或SORT_DESC
* @param string $arg3 排序的方法 SORT_REGULAR
* @return array
*/
function getArraySort($arr, $arg1, $arg2 = "SORT_ASC", $arg3 = "SORT_REGULAR")
{
if (!is_array($arr) || !$arr)
{
return $arr;
}
$argcount = func_num_args();
for ($i = 1; $i < $argcount; $i++)
{
$arg = func_get_arg($i);
if (!preg_match("/SORT_(.*)/i", $arg))
{
$keynamelist[] = $arg;
$sortrule[] = '$' . $arg;
}
else
{
$sortrule[] = $arg;
}
}
foreach ($arr AS $key => $info)
{
foreach ($keynamelist AS $keyname)
{
${$keyname}[$key] = $info[$keyname];
}
}
$evalstring = 'array_multisort(' . join(",", $sortrule) . ',$arr);';
eval($evalstring);
return $arr;
}
迷茫2017-05-16 13:10:20
$beforeSort = [
"0" => ["name" => "张三", "english" => 80, "chinese" => 60, "math" => 50 ],
"1" => ["name" => "李四", "english" => 50, "chinese" => 60, "math" => 70 ],
"2" => ["name" => "老王", "english" => 30, "chinese" => 50, "math" => 80 ],
];
$arr = array();
foreach($beforeSort as $value) {
$arr[$value['chinese']][$value['math']] = $value;
}
sort($arr);
$result = array();
foreach($arr as $val) {
sort($val);
foreach($val as $vo) {
$result[] = $vo;
}
}
var_dump($result);die;
Cetak hasil:
array(3) {
[0]=>
array(4) {
["name"]=>
string(6) "老王"
["english"]=>
int(30)
["chinese"]=>
int(50)
["math"]=>
int(80)
}
[1]=>
array(4) {
["name"]=>
string(6) "张三"
["english"]=>
int(80)
["chinese"]=>
int(60)
["math"]=>
int(50)
}
[2]=>
array(4) {
["name"]=>
string(6) "李四"
["english"]=>
int(50)
["chinese"]=>
int(60)
["math"]=>
int(70)
}
}
大家讲道理2017-05-16 13:10:20
Anda boleh menukar tatasusunan kepada set dan kemudian memprosesnya. Kaedah isihan yang dilaksanakan menggunakan koleksi PHP mengkhususkan diri dalam pelbagai pengisihan kompleks
PHPz2017-05-16 13:10:20
<?php
//Sekarang anda perlu mengikut susunan bahasa Cina dalam tatasusunan Jika ia adalah sama, ikut susunan matematik Hasil akhir mestilah tatasusunan berikut:
$beforeSort = [
"0" => ["name" => "张三", "english" => 80, "chinese" => 60, "math" => 50 ],
"1" => ["name" => "李四", "english" => 50, "chinese" => 60, "math" => 70 ],
"2" => ["name" => "老王", "english" => 30, "chinese" => 50, "math" => 80 ],
];
$data_math = array_column($beforeSort, 'math'); );
我想大声告诉你2017-05-16 13:10:20
$beforeSort = [
"0" => ["name" => "张三", "english" => 80, "chinese" => 60, "math" => 80 ],
"1" => ["name" => "李四", "english" => 50, "chinese" => 60, "math" => 70 ],
"2" => ["name" => "老王", "english" => 30, "chinese" => 50, "math" => 80 ],
];
usort($beforeSort, function($a, $b) {
return [$a['chinese'], $a['math']] <=> [$b['chinese'], $b['math']];
});
var_dump($beforeSort);
大家讲道理2017-05-16 13:10:20
///Meminjam jawapan dari lelaki di tingkat atas
usort($beforeSort, function ($a, $b) {
return [$a['chinese'], $b['math']] <=> [$b['chinese'], $a['math']];
});
某草草2017-05-16 13:10:20
Untuk menyusun tatasusunan berbilang dimensi, terdapat fungsi rasmi yang boleh melaksanakan array_multisort
伊谢尔伦2017-05-16 13:10:20
$beforeSort = [
"0" => ["name" => "张三", "english" => 80, "chinese" => 60, "math" => 80 ],
"1" => ["name" => "李四", "english" => 50, "chinese" => 60, "math" => 70 ],
"2" => ["name" => "老王", "english" => 30, "chinese" => 50, "math" => 80 ]
];
foreach( $beforeSort as $key => $value )
{
$chinese[$key] = $value['chinese'];
$math[$key] = $value['math'];
}
array_multisort( $chinese, SORT_ASC, $math, SORT_DESC, $beforeSort );
echo '<pre>';
print_r($beforeSort);
天蓬老师2017-05-16 13:10:20
$beforeSort = [
"0" => ["name" => "张三", "english" => 80, "chinese" => 60, "math" => 50 ],
"1" => ["name" => "李四", "english" => 50, "chinese" => 60, "math" => 70 ],
"2" => ["name" => "老王", "english" => 30, "chinese" => 50, "math" => 80 ],
];
foreach ($beforeSort as $key => $value) {
$chinese[$key] = $value['chinese'];
$math[$key] = $value['math'];
}
array_multisort($cina, SORT_ASC, $math, SORT_ASC, $beforeSort);
print_r($beforeSort);