green123
green123

Reputation: 75

array_values() doesn't work as expected

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.

Upvotes: 0

Views: 3882

Answers (1)

Daniel Alder
Daniel Alder

Reputation: 5382

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

Upvotes: 2

Related Questions