PHP 转换数组键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6446942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:24:36  来源:igfitidea点击:

PHP Convert Array Keys

phparrays

提问by grep

Had a hard time wording this in google so I figure ill ask here.

很难在谷歌中措辞,所以我想在这里问。

I have an array like such:

我有一个这样的数组:

Array ( [a] => 'a' [b] => 'b' [c] => 'c' )

Is there an easy way to convert the keys to numerical values like such? Is there a built in function, or will I have to make one?

有没有一种简单的方法可以将键转换为这样的数值?是否有内置功能,还是我必须制作一个?

Array ( [0] => 'a' [1] => 'b' [2] => 'c' )

Thanks!

谢谢!

回答by borrible

You want the array_valuesfunction.

您需要array_values函数。

回答by Tom Imrei

Use array_values(), as in the manual example:

使用array_values(),如手册示例中所示:

<?php
$array = array("size" => "XL", "color" => "gold");
print_r(array_values($array));

The above example will output:

上面的例子将输出:

Array
(
    [0] => XL
    [1] => gold
)

回答by BRampersad

Try array_values

尝试 array_values

$arrayWithNumericKeys = array_values($arrayWithRegularKeys);

回答by FinalForm

$my_array = array( [a] => 'a' [b] => 'b' [c] => 'c' )
$my_new_array = array_values($my_array);