Dok
Dok

Reputation: 39

PHP - merge values from array with same key and different values

I've got the following array:

Array ( [0] => something [1] => anotherhing )

What i want to obtain is :

Array ( [0] => [something, anotherthing] )

Thank you!

Upvotes: 0

Views: 71

Answers (1)

Arndt H. Ziegler
Arndt H. Ziegler

Reputation: 88

If you have $original_arr = ['something', 'anotherthing'] and you want a new array that has this array as value in first position, simply do $new_arr = [$original_arr]. This assumes all involved arrays are non-associative, which seems to be the case according to your question.

However, if your goal is the have an array whose first value is a string composed from the entries of the first array, do this:

$str = '[';
$str .= implode(', ', $original_arr);
$str .= ']';
$new_arr = [$str];

Updated solution with implode, thanks to @Jon.

Upvotes: 1

Related Questions