140

问题是如何为数组重置密钥:

Array ( 
    [1_Name] => Array ( 
        [1] => leo 
        [4] => NULL 
    ) 
    [1_Phone] => Array ( 
        [1] => 12345 
        [4] => 434324
    )  
)

重置为:

Array ( 
    [1_Name] => Array ( 
        [0] => leo 
        [1] => NULL 
    ) 
    [1_Phone] => Array ( 
        [0] => 12345 
        [1] => 434324
    ) 
)
4

6 回答 6

321

重置数组中所有数组的键:

$arr = array_map('array_values', $arr);

如果您只想重置第一级数组键,请使用array_values()不带array_map.

于 2012-05-08T05:09:23.713 回答
217
$array[9] = 'Apple';
$array[12] = 'Orange';
$array[5] = 'Peach';

$array = array_values($array);

通过此功能,您可以重置您的阵列

$array[0] = 'Apple';
$array[1] = 'Orange';
$array[2] = 'Peach';
于 2012-05-08T05:11:31.450 回答
21

用于array_values重置密钥

foreach($input as &$val) {
   $val = array_values($val);
}

http://php.net/array_values

于 2012-05-08T05:09:08.417 回答
11

在这里,您可以看到deceze提供的方式与简单array_values方法之间的区别:

数组:

$array['a'][0] = array('x' => 1, 'y' => 2, 'z' => 3);
$array['a'][5] = array('x' => 4, 'y' => 5, 'z' => 6);

$array['b'][1] = array('x' => 7, 'y' => 8, 'z' => 9);
$array['b'][7] = array('x' => 10, 'y' => 11, 'z' => 12);

顺便说一下,这deceze是您的输出:

$array = array_map('array_values', $array);
print_r($array);

/* Output */

Array
(
    [a] => Array
        (
            [0] => Array
                (
                    [x] => 1
                    [y] => 2
                    [z] => 3
                )
            [1] => Array
                (
                    [x] => 4
                    [y] => 5
                    [z] => 6
                )
        )
    [b] => Array
        (
            [0] => Array
                (
                    [x] => 7
                    [y] => 8
                    [z] => 9
                )

            [1] => Array
                (
                    [x] => 10
                    [y] => 11
                    [z] => 12
                )
        )
)

如果您只使用array_values函数,这是您的输出:

$array = array_values($array);
print_r($array);

/* Output */

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [x] => 1
                    [y] => 2
                    [z] => 3
                )
            [5] => Array
                (
                    [x] => 4
                    [y] => 5
                    [z] => 6
                )
        )
    [1] => Array
        (
            [1] => Array
                (
                    [x] => 7
                    [y] => 8
                    [z] => 9
                )
            [7] => Array
                (
                    [x] => 10
                    [y] => 11
                    [z] => 12
                )
        )
)
于 2013-04-05T07:39:48.897 回答
6
$result = ['5' => 'cherry', '7' => 'apple'];
array_multisort($result, SORT_ASC);
print_r($result);

数组( [0] => 苹果 [1] => 樱桃)

//...
array_multisort($result, SORT_DESC);
//...

数组( [0] => 樱桃 [1] => 苹果)

于 2015-08-19T15:51:51.713 回答
-7

为此存在 PHP 本机函数。见http://php.net/manual/en/function.reset.php

只需这样做:mixed reset ( array &$array )

于 2015-03-29T13:35:24.063 回答