PHP Aligning Array Key Values

前端 未结 3 1070
春和景丽
春和景丽 2021-01-16 20:53

I\'ve Googled it for two days, and tried looking at the PHP manual, and I still can\'t remember that function that aligns the key values for PHP arrays.

All I\'m loo

相关标签:
3条回答
  • 2021-01-16 21:11
    function array_reset_index_keys($array)
    {
        $return = array();foreach($array as $k => $v){$return[] = $v;}return $return;
    }
    

    And then use like a regular function, should re index the array

    you can also use native functions such as array_values which returns the values of an array into a single dimension array, causing it to be re indexed .

    0 讨论(0)
  • 2021-01-16 21:23

    You can use array_merge:

    $array = array_merge($array);
    

    It will reindex values with numeric keys.

    Update: Using array_values as proposed in @LostInTheCode's answer is probably more descriptive.

    0 讨论(0)
  • 2021-01-16 21:28

    Well, you see, this one is hard, because the general description on the PHP array functions page does not say that this function does what you're looking for.

    But you can sort the array using ksort(), and then use this: array_values() . From the page from the PHP manual:

    array_values() returns all the values from the input array and indexes numerically the array.

    0 讨论(0)
提交回复
热议问题