0

I'm looking for a solution to add an dimension to a php array. As this title is confusing Ill give you an example. If I have the array:

[0] => 'xx1',
[1] => 'xx2',
[2] => 'xx3',
...

I want to convert it to

[0] => 'y' => 'xx1',
[1] => 'y' => 'xx2',
[2] => 'y' => 'xx3',

Is there a way to do this without using a loop?

1 Answer 1

0

There's not a way to do it without touching each element, but with array_map you can do it without explicitly using a loop.

$new_array = array_map(function($x) {
    return ['y' => $x];
}, $your_array);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I think that's the best way to do it.

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.