我正在尝试重新索引和排序多维PHP数组,请记住,如果某些内部数组没有特定的索引,而其他内部数组则没有,我们只想跳到该特定内部数组中的下一个索引
例如,下面的源数组在subject_1和subject_3中没有数组索引4,但是在subject_2中具有数组索引4
Array
(
[subject_1] => Array
(
[2] => a
[6] => b
)
[subject_2] => Array
(
[2] => c
[4] => d
[6] => e
)
[subject_3] => Array
(
[2] => f
[6] => g
)
)
因此,预期结果数组应类似于以下内容
Array
(
[subject_1] => Array
(
[0] => a
[2] => b
)
[subject_2] => Array
(
[0] => c
[1] => d
[2] => e
)
[subject_3] => Array
(
[0] => f
[2] => g
)
)
)
)
以下代码的问题是,它在排序之前确实知道索引是否在所有子数组上都存在
$all_incoming_keys = array();
foreach($p2 as $key => $value){
$all_incoming_keys[] = $key;
}
for($sp2 = 0; $sp2 < count($p2); $sp2++){
sort($p2[$all_incoming_keys[$sp2]]);
array_values($p2[$all_incoming_keys[$sp2]]);
}
此代码为情况提供了正确的方法,或者我缺少一个功能
为了能够根据位置重新索引键是唯一键的列表,您首先需要确定此顺序。此代码遍历所有subjects