我想基于第一个主阵列对所有php数组进行排序。
这是我想用来对所有其他数组进行排序的主数组:
Array (
[0] => 10
[1] => 14
[2] => 15
[3] => 20
[4] => 21
[5] => 24
[6] => 25
[7] => 28
[8] => 30
[9] => 45
[10] => 60
[11] => 90
[12] => 120
[13] => 150
[14] => 180
[15] => 210
[16] => 240
[17] => 270
[18] => 365
)
这是需要排序的数组:
Array (
[0] => Array
(
[14] => 49.21
[20] => 71.04
[25] => 89.58
[30] => 100.00
)
[1] => Array
(
[180] => 412.00
[150] => 347.00
[120] => 285.00
[90] => 224.00
[60] => 165.00
[30] => 100.00
[14] => 47.00
)
)
我需要最终结果如下:
Array (
[0] => Array
(
[10] => n/a
[14] => 49.21
[15] => n/a
[20] => 71.04
[21] => n/a
[24] => n/a
[25] => 89.58
[28] => n/a
[30] => 100.00
[45] => n/a
[60] => n/a
[90] => n/a
[120] => n/a
[150] => n/a
[180] => n/a
[210] => n/a
[240] => n/a
[270] => n/a
[365] => n/a
)
[1] => Array
(
[10] => n/a
[14] => 71.04
[15] => n/a
[20] => n/a
[21] => n/a
[24] => n/a
[25] => n/a
[28] => n/a
[30] => 100.00
[45] => n/a
[60] => 165.00
[90] => 224.00
[120] => 285.00
[150] => 347.00
[180] => 412.00
[210] => n/a
[240] => n/a
[270] => n/a
[365] => n/a
)
)
感谢。
答案 0 :(得分:1)
假设你的初始数组是$ source,而$ todo是你的第二个包含两个子集的数组,那么:
$keys = array_flip($todo);
$keys = array_map(function() { return 'n/a'; }, $keys); // set all values to be "n/a";
foreach($todo as $idx => $do) {
$todo[$idx] = $do + $keys; // merge without renumbering.
}
答案 1 :(得分:0)
如果我从你的问题中理解了这一点,你想要使两个数组长度相同,并将不在其中的键设置为"n/a"
。
变体与array_merge
(Demo):
$shorten = array(
0 => 10,
1 => 14,
2 => 15,
3 => 20,
4 => 21,
5 => 24,
6 => 25,
7 => 28,
8 => 30,
9 => 45,
10 => 60,
11 => 90,
12 => 120,
13 => 150,
14 => 180,
15 => 210,
16 => 240,
17 => 270,
18 => 365,
);
$data = array(
0 => array(
14 => '49.21',
20 => '71.04',
25 => '89.58',
30 => '100.00',
),
1 => array(
180 => '412.00',
150 => '347.00',
120 => '285.00',
90 => '224.00',
60 => '165.00',
30 => '100.00',
14 => '47.00',
),
);
// default array as the base
$shorten = array_combine($shorten, array_fill(0, count($shorten), 'n/a'));
foreach($data as &$array) {
// merge to get set members
$array = array_merge($shorten, $array);
}
unset($array);
var_dump($data);
结果:
array(2) {
[0]=>
array(23) {
[0]=>
string(3) "n/a"
[1]=>
string(3) "n/a"
[2]=>
string(3) "n/a"
[3]=>
string(3) "n/a"
[4]=>
string(3) "n/a"
[5]=>
string(3) "n/a"
[6]=>
string(3) "n/a"
[7]=>
string(3) "n/a"
[8]=>
string(3) "n/a"
[9]=>
string(3) "n/a"
[10]=>
string(3) "n/a"
[11]=>
string(3) "n/a"
[12]=>
string(3) "n/a"
[13]=>
string(3) "n/a"
[14]=>
string(3) "n/a"
[15]=>
string(3) "n/a"
[16]=>
string(3) "n/a"
[17]=>
string(3) "n/a"
[18]=>
string(3) "n/a"
[19]=>
string(5) "49.21"
[20]=>
string(5) "71.04"
[21]=>
string(5) "89.58"
[22]=>
string(6) "100.00"
}
[1]=>
array(26) {
[0]=>
string(3) "n/a"
[1]=>
string(3) "n/a"
[2]=>
string(3) "n/a"
[3]=>
string(3) "n/a"
[4]=>
string(3) "n/a"
[5]=>
string(3) "n/a"
[6]=>
string(3) "n/a"
[7]=>
string(3) "n/a"
[8]=>
string(3) "n/a"
[9]=>
string(3) "n/a"
[10]=>
string(3) "n/a"
[11]=>
string(3) "n/a"
[12]=>
string(3) "n/a"
[13]=>
string(3) "n/a"
[14]=>
string(3) "n/a"
[15]=>
string(3) "n/a"
[16]=>
string(3) "n/a"
[17]=>
string(3) "n/a"
[18]=>
string(3) "n/a"
[19]=>
string(6) "412.00"
[20]=>
string(6) "347.00"
[21]=>
string(6) "285.00"
[22]=>
string(6) "224.00"
[23]=>
string(6) "165.00"
[24]=>
string(6) "100.00"
[25]=>
string(5) "47.00"
}
}
具有映射功能的变体(Demo):
$shorten = array(
0 => 10,
1 => 14,
2 => 15,
3 => 20,
4 => 21,
5 => 24,
6 => 25,
7 => 28,
8 => 30,
9 => 45,
10 => 60,
11 => 90,
12 => 120,
13 => 150,
14 => 180,
15 => 210,
16 => 240,
17 => 270,
18 => 365,
);
// overload $shorten array with a mapping function
$shorten = function(array $a) use ($shorten)
{
$r = array();
foreach($shorten as $i => $k)
{
$r[$k] = isset($a[$k]) ? $a[$k] : 'n/a';
}
return $r;
};
$data = array(
0 => array(
14 => '49.21',
20 => '71.04',
25 => '89.58',
30 => '100.00',
),
1 => array(
180 => '412.00',
150 => '347.00',
120 => '285.00',
90 => '224.00',
60 => '165.00',
30 => '100.00',
14 => '47.00',
),
);
// apply mapping function to $data
$data = array_map($shorten, $data);
var_dump($data); # result
结果:
array(2) {
[0]=>
array(19) {
[10]=>
string(3) "n/a"
[14]=>
string(5) "49.21"
[15]=>
string(3) "n/a"
[20]=>
string(5) "71.04"
[21]=>
string(3) "n/a"
[24]=>
string(3) "n/a"
[25]=>
string(5) "89.58"
[28]=>
string(3) "n/a"
[30]=>
string(6) "100.00"
[45]=>
string(3) "n/a"
[60]=>
string(3) "n/a"
[90]=>
string(3) "n/a"
[120]=>
string(3) "n/a"
[150]=>
string(3) "n/a"
[180]=>
string(3) "n/a"
[210]=>
string(3) "n/a"
[240]=>
string(3) "n/a"
[270]=>
string(3) "n/a"
[365]=>
string(3) "n/a"
}
[1]=>
array(19) {
[10]=>
string(3) "n/a"
[14]=>
string(5) "47.00"
[15]=>
string(3) "n/a"
[20]=>
string(3) "n/a"
[21]=>
string(3) "n/a"
[24]=>
string(3) "n/a"
[25]=>
string(3) "n/a"
[28]=>
string(3) "n/a"
[30]=>
string(6) "100.00"
[45]=>
string(3) "n/a"
[60]=>
string(6) "165.00"
[90]=>
string(6) "224.00"
[120]=>
string(6) "285.00"
[150]=>
string(6) "347.00"
[180]=>
string(6) "412.00"
[210]=>
string(3) "n/a"
[240]=>
string(3) "n/a"
[270]=>
string(3) "n/a"
[365]=>
string(3) "n/a"
}
}