0

I have such array with different keys and values

$input = array(
    "a" => "green",
    "red",
    "b" => "green",
    'people' => array('Jane', 'Sam'),
    array(
        'fruits' => array('orange', 'banana', 'apple'),
        'veggie' => array('carrot', 'collard', 'pea')
    )
);
var_dump(array_values($input));

The result is

array (size=5)
  0 => string 'green' (length=5)
  1 => string 'red' (length=3)
  2 => string 'green' (length=5)
  3 => 
    array (size=3)                         <===HERE was reindexed
       0 => string 'Jane' (length=4)
       1 => string 'Sam' (length=3)
  4 => 
    array (size=2)
      'fruits' =>                          <===HERE wasn't reindexed
        array (size=3)
          0 => string 'orange' (length=6)
          1 => string 'banana' (length=6)
          2 => string 'apple' (length=5)
      'veggie' =>                          <===HERE wasn't reindexed
        array (size=3)
          0 => string 'carrot' (length=6)
          1 => string 'collard' (length=7)
          2 => string 'pea' (length=3)

Why keys fruits and veggie was not reindexed but people was reindexed? What is the logic.

4
  • 'people' wasn't reindexed..... it was already indexed in that way before the array_values() call Commented Oct 22, 2015 at 12:32
  • The logic is, that the function doesn't work for multidimensional arrays Commented Oct 22, 2015 at 12:32
  • 2
    Like most array functions, array_values() isn't recursive Commented Oct 22, 2015 at 12:33
  • I wonder why the array containing Jane and Sam is shown as array (size=3) (note the size of 3 instead of 2). Looks like manual modification... Commented Oct 22, 2015 at 13:00

1 Answer 1

2

Did you ever look at $input without array_values()? Fact is that array_values only reindexes the first level - the rest doesn't change (=not recursive). You just forgot that array('Jane', 'Sam') gets automatically expanded to array(0=>'Jane', 1=>'Sam').

This gets obvious if you diff the two outputs:

echo "== before.txt ==\n";
var_dump($input);
echo "== after.txt ==\n";
var_dump(array_values($input));

The result

$ sdiff before.txt after.txt
array(5) {                              array(5) {
  ["a"]=>                             |   [0]=>
  string(5) "green"                       string(5) "green"
  [0]=>                               |   [1]=>
  string(3) "red"                         string(3) "red"
  ["b"]=>                             |   [2]=>
  string(5) "green"                       string(5) "green"
  ["people"]=>                        |   [3]=>
  array(2) {                              array(2) {
    [0]=>                                   [0]=>
    string(4) "Jane"                        string(4) "Jane"
    [1]=>                                   [1]=>
    string(3) "Sam"                         string(3) "Sam"
  }                                       }
  [1]=>                               |   [4]=>
  array(2) {                              array(2) {
    ["fruits"]=>                            ["fruits"]=>
    array(3) {                              array(3) {
      [0]=>                                   [0]=>
      string(6) "orange"                      string(6) "orange"
      [1]=>                                   [1]=>
      string(6) "banana"                      string(6) "banana"
      [2]=>                                   [2]=>
      string(5) "apple"                       string(5) "apple"
    }                                       }
    ["veggie"]=>                            ["veggie"]=>
    array(3) {                              array(3) {
      [0]=>                                   [0]=>
      string(6) "carrot"                      string(6) "carrot"
      [1]=>                                   [1]=>
      string(7) "collard"                     string(7) "collard"
      [2]=>                                   [2]=>
      string(3) "pea"                         string(3) "pea"
    }                                       }
  }                                       }
}                                       }
Sign up to request clarification or add additional context in comments.

Comments

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.