0

I need to make an array that looks like this:

$array = [
      'value_1' => [
        'key' => 'name_1'
      ],
      'value_2' => [
        'key' => 'name_2'
      ]
    ];

From two different array that look like this:

$array_two=['value_1', 'value_2']; $array_three=['name_1', 'name_2'];

1 Answer 1

1
$array_two = ['value_1', 'value_2'];
$array_three = ['name_1', 'name_2'];

$array = array_combine($array_two, array_map(function ($val) {
    return ['key' => $val];
}, $array_three));

print_r($array);
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you man!! You are the best!!! Is there a way to avoid error like array_combine(): Both parameters should have an equal number of elements if $array_three has more elements?
there is... but how should the result look like? seems to make no sense!?

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.