jmarais
jmarais

Reputation: 93

Concatenate arrays to use in json encoding

I have an array $options:

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

After using json_encode produce an output string like this:

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

but what I want is to add an entry at the beginning to have:

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

I created the following array:

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

and I used the following cancatenation methods:

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

but both produce the following:

{"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"}}

which contains these incremental numerical values (the actual array contains about 200 items).

How can I add the first line to get the desired uotput, i.e.:

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

Upvotes: 1

Views: 1053

Answers (1)

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324640

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

array_unshift()

Upvotes: 1

Related Questions