0

I've got the following array:

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

What i want to obtain is :

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

Thank you!

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This is reimplementing implode, there's no need for that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.