写了这个递归函数来重新索引数组(参见下图 - 我使用的JSONized数组,以便更好地看到它)
代码:
private function reindex($array)
{
foreach ($array as $key => $value) {
if (is_array($value)) {
$array[$key]=$this->reindex($value);
}
array_values($array);
}
return $array;
}
目标是重新索引它,使索引没有跳跃,即
$ arr [' body'] [0] [' 1'] [' body']包含索引2,4和6的元素,希望将其重新索引为0,1,2
也适用于不同的数组(n个嵌套数组)。应该是一般功能。
我该怎么办?
谢谢,
更新1:
Var_dump输出:
array(4) {
["token_name"]=>
string(6) "C_ROOT"
["token_group"]=>
string(7) "C_BLOCK"
["group"]=>
bool(true)
["body"]=>
array(1) {
[0]=>
array(2) {
[0]=>
array(7) {
["token_name_org"]=>
string(8) "T_SWITCH"
["token"]=>
int(339)
["value"]=>
string(6) "switch"
["line"]=>
int(2)
["token_group"]=>
string(9) "FUNCTIONS"
["token_name"]=>
string(8) "C_SWITCH"
["args"]=>
array(1) {
[0]=>
array(1) {
[0]=>
array(7) {
["token_name_org"]=>
string(10) "T_VARIABLE"
["token"]=>
int(320)
["value"]=>
string(4) "argv"
["line"]=>
int(2)
["token_group"]=>
string(9) "VARIABLES"
["token_name"]=>
string(10) "C_VARIABLE"
["args"]=>
array(1) {
[0]=>
array(1) {
[0]=>
array(6) {
["token_name_org"]=>
string(9) "T_LNUMBER"
["token"]=>
int(317)
["value"]=>
string(1) "1"
["line"]=>
int(2)
["token_group"]=>
string(9) "VARIABLES"
["token_name"]=>
string(8) "C_NUMBER"
}
}
}
}
}
}
}
[1]=>
array(4) {
["token_name"]=>
string(7) "C_BLOCK"
["token_group"]=>
string(7) "C_BLOCK"
["group"]=>
bool(true)
["body"]=>
array(4) {
[0]=>
array(1) {
[0]=>
array(4) {
["token_name"]=>
string(12) "C_CASE_BLOCK"
["token_group"]=>
string(12) "C_CASE_BLOCK"
["group"]=>
bool(true)
["args"]=>
array(1) {
[0]=>
array(6) {
["token_name_org"]=>
string(9) "T_LNUMBER"
["token"]=>
int(317)
["value"]=>
string(2) "10"
["line"]=>
int(4)
["token_group"]=>
string(9) "VARIABLES"
["token_name"]=>
string(8) "C_NUMBER"
}
}
}
}
[2]=>
array(1) {
[0]=>
array(5) {
["token_name"]=>
string(12) "C_CASE_BLOCK"
["token_group"]=>
string(12) "C_CASE_BLOCK"
["group"]=>
bool(true)
["args"]=>
array(1) {
[0]=>
array(6) {
["token_name_org"]=>
string(26) "T_CONSTANT_ENCAPSED_STRING"
["token"]=>
int(323)
["value"]=>
string(13) "single_quoted"
["line"]=>
int(7)
["token_group"]=>
string(7) "STRINGS"
["token_name"]=>
string(8) "C_STRING"
}
}
["body"]=>
array(1) {
[0]=>
array(5) {
[0]=>
array(6) {
["token_name_org"]=>
string(10) "T_VARIABLE"
["token"]=>
int(320)
["value"]=>
string(3) "val"
["line"]=>
int(8)
["token_group"]=>
string(9) "VARIABLES"
["token_name"]=>
string(10) "C_VARIABLE"
}
[1]=>
array(6) {
["token_name_org"]=>
string(18) "C_ASSIGNMENT_EQUAL"
["line"]=>
int(8)
["value"]=>
string(1) "="
["token"]=>
string(5) "VALUE"
["token_group"]=>
string(11) "ASSIGNMENTS"
["token_name"]=>
string(18) "C_ASSIGNMENT_EQUAL"
}
[2]=>
array(6) {
["token_name_org"]=>
string(9) "T_LNUMBER"
["token"]=>
int(317)
["value"]=>
string(1) "3"
["line"]=>
int(8)
["token_group"]=>
string(9) "VARIABLES"
["token_name"]=>
string(8) "C_NUMBER"
}
[3]=>
array(6) {
["token_name_org"]=>
string(7) "VALUE_-"
["line"]=>
int(8)
["value"]=>
string(1) "-"
["token"]=>
string(5) "VALUE"
["token_group"]=>
string(9) "OPERATORS"
["token_name"]=>
string(16) "C_OPERATOR_MINUS"
}
[4]=>
array(6) {
["token_name_org"]=>
string(9) "T_LNUMBER"
["token"]=>
int(317)
["value"]=>
string(1) "5"
["line"]=>
int(8)
["token_group"]=>
string(9) "VARIABLES"
["token_name"]=>
string(8) "C_NUMBER"
}
}
}
}
}
[4]=>
array(1) {
[0]=>
array(5) {
["token_name"]=>
string(12) "C_CASE_BLOCK"
["token_group"]=>
string(12) "C_CASE_BLOCK"
["group"]=>
bool(true)
["args"]=>
array(1) {
[0]=>
array(6) {
["token_name_org"]=>
string(26) "T_CONSTANT_ENCAPSED_STRING"
["token"]=>
int(323)
["value"]=>
string(13) "double_quoted"
["line"]=>
int(11)
["token_group"]=>
string(7) "STRINGS"
["token_name"]=>
string(8) "C_STRING"
}
}
["body"]=>
array(1) {
[0]=>
array(1) {
[0]=>
array(7) {
["token_name_org"]=>
string(6) "T_ECHO"
["token"]=>
int(328)
["value"]=>
string(4) "echo"
["line"]=>
int(13)
["token_group"]=>
string(9) "FUNCTIONS"
["token_name"]=>
string(6) "C_ECHO"
["args"]=>
array(1) {
[0]=>
array(1) {
[0]=>
array(6) {
["token_name_org"]=>
string(26) "T_CONSTANT_ENCAPSED_STRING"
["token"]=>
int(323)
["value"]=>
string(13) "double quoted"
["line"]=>
int(13)
["token_group"]=>
string(7) "STRINGS"
["token_name"]=>
string(8) "C_STRING"
}
}
}
}
}
}
}
}
[6]=>
array(3) {
[0]=>
array(4) {
["token_name_org"]=>
string(9) "T_DEFAULT"
["token"]=>
int(342)
["value"]=>
string(7) "default"
["line"]=>
int(18)
}
[1]=>
array(6) {
["token_name_org"]=>
string(17) "C_SEPARATOR_COLON"
["line"]=>
int(18)
["value"]=>
string(1) ":"
["token"]=>
string(5) "VALUE"
["token_group"]=>
string(10) "SEPARATORS"
["token_name"]=>
string(17) "C_SEPARATOR_COLON"
}
[2]=>
array(6) {
["token_name_org"]=>
string(7) "T_BREAK"
["token"]=>
int(343)
["value"]=>
string(5) "break"
["line"]=>
int(19)
["token_group"]=>
string(16) "TODO_BREAK_GROUP"
["token_name"]=>
string(7) "C_BREAK"
}
}
}
}
}
}
}
答案 0 :(得分:1)
这是一种只重新索引数值的方法:
function reindex($array)
{
$index = 0;
$return = [];
foreach ($array as $key => $value) {
if (is_string($key)) {
$newKey = $key;
} else {
$newKey = $index;
++$index;
}
$return[$newKey] = is_array($value) ? reindex($value) : $value;
}
// Sort alphabetically, numeric first then alpha
ksort($return, SORT_NATURAL);
return $return;
}