0

Wrote this recursive function to reindex an array (see image below - JSONized Array I work with, to better see it)

Array JSONized

Code:

 private function reindex($array)
 {
        foreach ($array as $key => $value) {

            if (is_array($value)) {

                $array[$key]=$this->reindex($value);

            }

            array_values($array);



        }

        return $array;


 }

The goal is to reindex it so there are no jump in indexes i.e

$arr['body'][0]['1']['body'] has elements at indexes 2, 4 and 6, would like to reindex it to be 0, 1, 2

Should work for different arrays also (n amount of nested arrays). Should be general function.

How can I do it?

Thanks,

Update 1:

Var_dump output:

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"
            }
          }
        }
      }
    }
  }
}
4
  • Are you attempting to keep string keys or you want the entire thing to be numeric? Commented Jun 12, 2017 at 14:18
  • I was thinking to reindex only numeric keys ... string keys should stay as they are. Nice would be to maybe sort them alphabetically, but this is optional .... Commented Jun 12, 2017 at 14:22
  • @dev please always present your array-type data as var_export() so that volunteers can instantly use your data in their demo/test scripts. I plan to offer an answer, but first I will need to completely reformat your data so that I can use it. Commented Oct 5, 2020 at 13:04
  • @dev Stack Overflow also asks that you provide a minimal reproducible example. Your sample array could be much smaller if you removed many of the irrelevant associative arrays (without damaging the relevant structure). Commented Oct 5, 2020 at 13:22

1 Answer 1

3

Here is a method that will only reindex numeric values:

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;
}

Example here: http://ideone.com/969OGa

Sign up to request clarification or add additional context in comments.

1 Comment

No worries, if you want to sort alpha then numeric use ksort($return, SORT_NUMERIC);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.