1

I have array 1 like this

Array
(
    [0] => 1
    [1] => 2
)

Second array would be

Array
(
    [0] => Array
        (
            [FullName] => Bhupat Chippa
        )

    [1] => Array
        (
            [FullName] => Dvs Patel
        )
)

I want to merge it the way values would be added to second array with same keys. Desired Output will look like this or some way around so that I can use the Array 1's value with Second Array Only:

Array
(
    [0] => Array
        (
            [FullName] => Bhupat Chippa
            [0] => 1
        )

    [1] => Array
        (
            [FullName] => Dvs Patel
            [1] => 2
        )

)
3
  • A simple foreach() loop would do the trick. Try that and see if it works. Commented May 26, 2018 at 3:54
  • Down voted because This is simple and not added any effort. Commented May 26, 2018 at 4:27
  • actually it had a lot to do with database, i just simplified the question for understandability, any way I appreciate the help, thanks! Commented May 26, 2018 at 4:36

2 Answers 2

2

You can apply simple foreach() to do that

$final = [];

foreach($array2 as $key =>$arr2 ){
  $final[$key]['FullName'] = $arr2['FullName'];
  $final[$key][$key] = $array1[$key];
}

print_r($final);

Output:- https://eval.in/1010437

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

2 Comments

I up voted your answer, but please don't encourage people to ask without try or any effort.
Actually one answer is given which don't give desired result, that's why i give another solution
0

If both arrays are of the same length, you might use array_map passing the array_keys as the second parameter:

$array1 = ["1", "2"];
$array2 = [
    ["FullName" => "Bhupat Chippa"],
    ["FullName" => "Dvs Patel"]
];

$result = array_map(function($x, $y) use ($array1){
    $x[$y] = $array1[$y];
    return $x;
}, $array2, array_keys($array1));

print_r($result);

Demo

That will give you:

Array
(
    [0] => Array
        (
            [FullName] => Bhupat Chippa
            [0] => 1
        )

    [1] => Array
        (
            [FullName] => Dvs Patel
            [1] => 2
        )

)

Comments

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.