我的多维数组$cs_map_data
如下:
Array
(
[2] => Array
(
[class_id] => 2
[class_name] => II
[class_checked] => 0
[class_subjects] => Array
(
[0] => Array
(
[cs_map_id] => 81
[subject_name] => 11 Engllish
[subject_checked] => 0
[teacher_cs_id] =>
)
)
)
[3] => Array
(
[class_id] => 3
[class_name] => III
[class_checked] => 0
[class_subjects] => Array
(
[0] => Array
(
[cs_map_id] => 155
[subject_name] => Hidi
[subject_checked] => 0
[teacher_cs_id] =>
)
[1] => Array
(
[cs_map_id] => 156
[subject_name] => 11 Maths
[subject_checked] => 0
[teacher_cs_id] =>
)
[2] => Array
(
[cs_map_id] => 157
[subject_name] => 11 Science
[subject_checked] => 0
[teacher_cs_id] =>
)
[3] => Array
(
[cs_map_id] => 158
[subject_name] => 11 Engllish
[subject_checked] => 0
[teacher_cs_id] =>
)
)
)
[4] => Array
(
[class_id] => 4
[class_name] => IV
[class_checked] => 0
[class_subjects] => Array
(
[0] => Array
(
[cs_map_id] => 86
[subject_name] => Physics
[subject_checked] => 0
[teacher_cs_id] =>
)
)
)
[6] => Array
(
[class_id] => 6
[class_name] => VI
[class_checked] => 0
[class_subjects] => Array
(
[0] => Array
(
[cs_map_id] => 144
[subject_name] => Mathematics
[subject_checked] => 0
[teacher_cs_id] =>
)
[1] => Array
(
[cs_map_id] => 145
[subject_name] => Biology
[subject_checked] => 0
[teacher_cs_id] =>
)
)
)
[7] => Array
(
[class_id] => 7
[class_name] => VII
[class_checked] => 0
[class_subjects] => Array
(
[0] => Array
(
[cs_map_id] => 129
[subject_name] => Physics
[subject_checked] => 0
[teacher_cs_id] =>
)
[1] => Array
(
[cs_map_id] => 130
[subject_name] => Chemistry11
[subject_checked] => 0
[teacher_cs_id] =>
)
[2] => Array
(
[cs_map_id] => 131
[subject_name] => 11 Science
[subject_checked] => 0
[teacher_cs_id] =>
)
)
)
[8] => Array
(
[class_id] => 8
[class_name] => VIII
[class_checked] => 0
[class_subjects] => Array
(
[0] => Array
(
[cs_map_id] => 67
[subject_name] => Hidi
[subject_checked] => 0
[teacher_cs_id] =>
)
[1] => Array
(
[cs_map_id] => 68
[subject_name] => 11 Engllish
[subject_checked] => 0
[teacher_cs_id] =>
)
)
)
[9] => Array
(
[class_id] => 9
[class_name] => IX
[class_checked] => 0
[class_subjects] => Array
(
[0] => Array
(
[cs_map_id] => 87
[subject_name] => Mathematics
[subject_checked] => 0
[teacher_cs_id] =>
)
[1] => Array
(
[cs_map_id] => 88
[subject_name] => Hidi
[subject_checked] => 0
[teacher_cs_id] =>
)
[2] => Array
(
[cs_map_id] => 89
[subject_name] => 11 Science
[subject_checked] => 0
[teacher_cs_id] =>
)
)
)
)
如果您注意到,数组的键是[2],[3],[4],[6],[7],[8],[9]
。这些键是从其他开发者定义的函数中得到的。现在我的要求是重命名这些键,即数组键应该是[0],[1],[2],[3],[4],[5],[6]
。其他数组元素和其他键值对应该保持原样。我在谷歌上搜索了一下,但找不到答案。谁能帮我重新排序一下这些数组键?提前感谢。我需要的输出如下:
Array
(
[0] => Array
(
[class_id] => 2
[class_name] => II
[class_checked] => 0
[class_subjects] => Array
(
[0] => Array
(
[cs_map_id] => 81
[subject_name] => 11 Engllish
[subject_checked] => 0
[teacher_cs_id] =>
)
)
)
[1] => Array
(
[class_id] => 3
[class_name] => III
[class_checked] => 0
[class_subjects] => Array
(
[0] => Array
(
[cs_map_id] => 155
[subject_name] => Hidi
[subject_checked] => 0
[teacher_cs_id] =>
)
[1] => Array
(
[cs_map_id] => 156
[subject_name] => 11 Maths
[subject_checked] => 0
[teacher_cs_id] =>
)
[2] => Array
(
[cs_map_id] => 157
[subject_name] => 11 Science
[subject_checked] => 0
[teacher_cs_id] =>
)
[3] => Array
(
[cs_map_id] => 158
[subject_name] => 11 Engllish
[subject_checked] => 0
[teacher_cs_id] =>
)
)
)
[2] => Array
(
[class_id] => 4
[class_name] => IV
[class_checked] => 0
[class_subjects] => Array
(
[0] => Array
(
[cs_map_id] => 86
[subject_name] => Physics
[subject_checked] => 0
[teacher_cs_id] =>
)
)
)
[3] => Array
(
[class_id] => 6
[class_name] => VI
[class_checked] => 0
[class_subjects] => Array
(
[0] => Array
(
[cs_map_id] => 144
[subject_name] => Mathematics
[subject_checked] => 0
[teacher_cs_id] =>
)
[1] => Array
(
[cs_map_id] => 145
[subject_name] => Biology
[subject_checked] => 0
[teacher_cs_id] =>
)
)
)
[4] => Array
(
[class_id] => 7
[class_name] => VII
[class_checked] => 0
[class_subjects] => Array
(
[0] => Array
(
[cs_map_id] => 129
[subject_name] => Physics
[subject_checked] => 0
[teacher_cs_id] =>
)
[1] => Array
(
[cs_map_id] => 130
[subject_name] => Chemistry11
[subject_checked] => 0
[teacher_cs_id] =>
)
[2] => Array
(
[cs_map_id] => 131
[subject_name] => 11 Science
[subject_checked] => 0
[teacher_cs_id] =>
)
)
)
[5] => Array
(
[class_id] => 8
[class_name] => VIII
[class_checked] => 0
[class_subjects] => Array
(
[0] => Array
(
[cs_map_id] => 67
[subject_name] => Hidi
[subject_checked] => 0
[teacher_cs_id] =>
)
[1] => Array
(
[cs_map_id] => 68
[subject_name] => 11 Engllish
[subject_checked] => 0
[teacher_cs_id] =>
)
)
)
[6] => Array
(
[class_id] => 9
[class_name] => IX
[class_checked] => 0
[class_subjects] => Array
(
[0] => Array
(
[cs_map_id] => 87
[subject_name] => Mathematics
[subject_checked] => 0
[teacher_cs_id] =>
)
[1] => Array
(
[cs_map_id] => 88
[subject_name] => Hidi
[subject_checked] => 0
[teacher_cs_id] =>
)
[2] => Array
(
[cs_map_id] => 89
[subject_name] => 11 Science
[subject_checked] => 0
[teacher_cs_id] =>
)
)
)
)
这只是Dave建议的array_values的问题。
在你的例子中,看起来你需要重新索引最外层的数组和'class_subjects'元素。
对于外层,这很容易。假设整个东西存储在变量$a
中,那么
$a = array_values($a);
和最外层的数组被重新索引!
内部数组更具挑战性,我建议尝试一下foreach,并为value组件使用引用。
同样,假设最外层的数组存储在$a
中并且已经被重新索引,现在
$a = array_values($a);
foreach($a as $i => &$sub)
$sub['class_subjects'] = array_values($sub['class_subjects']);
你就完成了
由于这里需要重新索引外部数组索引,因此array_values($arr)可以完美地使用它。
<?php
$arr = array(
'foo' => 'foo',
'bar' => array(
'baz' => 'baz',
'candy' => 'candy',
'vegetable' => array(
'carrot' => 'carrot',
)
),
'vegetable' => array(
'carrot' => 'carrot2',
),
'fruits' => 'fruits',
);
// Pass your array as argument
$arr1 = array_values($arr);
print_r($arr1);
?>
如果你想递归地迭代到内部数组,你可以参考array_values中给出的例子。希望它能解决你的问题。
print_r(array_values($array));