PHP:在一行中添加非数字值到范围数组的开头

PHP:在一行中添加非数字值到范围数组的开头

问题描述:

How can I add "Please choose" to the beginning of this array

$enDuration  =   range( 0 , 60);

Best would be it all happens in ONE line as it is for a language config file and should stay as clean as possible. Array_combine didn't work for me.

如何在此数组的开头添加“请选择” p>

  $ enDuration = range(0,60); 
  code>  pre> 
 
 

最好的一切都发生在一行,因为它是一个语言配置文件,应该 保持尽可能干净。 Array_combine对我不起作用。 p> div>

You can use array_merge, like this:

$enDuration = array_merge(['Please choose'], range(0, 60));

$enDuration  =   array_merge(array('Please choose'),range( 0 , 60));

Works!

Use

$enDuration = range( 0 , 60);
$enDuration = array('Please choose') + $enDuration; 

or

$enDuration = array('Please choose') + range( 0 , 60); 

you can use array function like this

$enDuration  =   array('Please choose',range( 0 , 60));
print_r($enDuration);

also you can use array_merge

$a=array_merge(array('Please choose'),range( 0 , 60));

but there is some difference between array_merge and array, you can check difference in below output,

your desired output

You can always create a new array() or merge with the array, but you can also just tell PHP to add it to the beginning of the array, and correct the keys automatically.

http://php.net/manual/en/function.array-unshift.php

$enDuration = range(0 , 60); array_unshift($enDuration, "Please choose");