PHP - 没有键的索引数组

3

我希望得到没有键的数组,

就像现在的数组:

array([1]=>'one', [2]=>'two', [3]=>'three');

但我需要这个数组没有键,就像这样:

array('one', 'two', 'three');

请告诉我应该怎样做,我需要将这个数组作为一个函数的参数,因此它必须没有键。

数组始终会有键。但如果您想使用从0开始的键重新索引数组,则请使用array_values - Amal Murali
它正在生成密钥,请检查示例。 - user007
你已经拥有了那个数组!所有的数组都有键。 - AlexP
有没有办法得到这样的数组:array('one', 'two', 'three'); - user007
2个回答

8
数组始终有键,无论你是否需要。即使是简单的 array('one', 'two', 'three'); 也会变成 array([0] => 'one', [1] => 'two', [2] => 'three');。话虽如此,你的数组确实以1开始,而不是以0开始(我猜这不是你想要的)。为了让数组从0开始,你可以使用 array_values 函数:
$new_array = array_values($old_array);

0
/*
Your array N/B make sure it is a legal array. So for it to be an array and 
produce this example  : array([1]=>'one', [2]=>'two', [3]=>'three');
We use the same of array with no index, since every array comes indexed from 0.
 */

$array          = array('one','two','three');

//final array variable 
$finalArray     = array();

//encode the arry to Json
$jsonArray      = json_encode($array);
//first search & replace
$firstReplace   = str_replace('[','array(',$jsonArray);
//last search & replace
$finalArray     = str_replace(']',')',$firstReplace);
//output
print_r($finalArray);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接