我希望得到没有键的数组,
就像现在的数组:
array([1]=>'one', [2]=>'two', [3]=>'three');
但我需要这个数组没有键,就像这样:
array('one', 'two', 'three');
请告诉我应该怎样做,我需要将这个数组作为一个函数的参数,因此它必须没有键。
array('one', 'two', 'three');
也会变成 array([0] => 'one', [1] => 'two', [2] => 'three');
。话虽如此,你的数组确实以1开始,而不是以0开始(我猜这不是你想要的)。为了让数组从0开始,你可以使用 array_values 函数:$new_array = array_values($old_array);
/*
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);
0
开始的键重新索引数组,则请使用array_values
。 - Amal Muraliarray('one', 'two', 'three');
? - user007