1

我有一个数组 $options:

$options = ('value' => '87', 'text' => 'Accessorize', 'image' =>'accessorize.ico'),('value' => '35', 'text' => 'Adams Kids', 'image' =>'AdamsKids.ico');

使用 json_encode 后生成如下输出字符串:

[{"value":"87","text":"Accessorize","image":"accessorize.ico"},{"value":"35","text":"Adams Kids","image":"AdamsKids.ico"}]

但我想要的是在开头添加一个条目:

[{"value":"0","text":"- Select Shop -","image":""},{"value":"87","text":"Accessorize","image":"accessorize.ico"},{"value":"35","text":"Adams Kids","image":"AdamsKids.ico"}]

我创建了以下数组:

$first = array('value' => '0', 'text' => '- Select Shop -', 'image' =>'');

我使用了以下连接方法:

$options2 = array_merge($first, $options);
$options2 = $first + $options;

但两者都产生以下内容:

{"value":"0","text":"- Select Shop -","image":"","0":{"value":"87","text":"Accessorize","image":"accessorize.ico"},"1":{"value":"35","text":"Adams Kids","image":"AdamsKids.ico"},"2":{"value":"92","text":"Alex and Alexa","image":"alexandalexa.ico"}}

其中包含这些增量数值(实际数组包含大约 200 个项目)。

如何添加第一行以获得所需的 uotput,即:

[{"value":"0","text":"- Select Shop -","image":""},{"value":"87","text":"Accessorize","image":"accessorize.ico"},{"value":"35","text":"Adams Kids","image":"AdamsKids.ico"}]
4

1 回答 1

1
$options2 = array_unshift($options,$first);

array_unshift()

于 2012-06-13T13:21:43.070 回答