array_values()没有按预期工作

时间:2015-10-22 12:31:30

标签: php function

我有不同键和值的数组

$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));

结果是

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)

为什么键水果素食没有重新编制索引但重新编制索引?逻辑是什么。

1 个答案:

答案 0 :(得分:1)

您是否曾在没有$input的情况下查看array_values()?事实上,array_values只重新索引第一级 - 其余的不会改变(=不递归)。您忘了array('Jane', 'Sam')自动扩展为array(0=>'Jane', 1=>'Sam')

如果您对两个输出进行区分,这一点就很明显了:

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

结果

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