1

I have 2 PHP arrays that I need to combine values together.

First Array

array(2) {
    [0]=>
    array(1) {
        ["id"]=>
        string(1) "1"
    }
    [1]=>
    array(1) {
        ["id"]=>
        string(2) "40"
    }
}

Second Array

array(2) {
    [0]=>
    string(4) "1008"
    [1]=>
    string(1) "4"
}

Output desired

array(2) {
    [0]=>
    array(1) {
        ["id"]=>
        string(1) "1",
        ["count"]=>
        string(1) "1008"
    }
    [1]=>
    array(1) {
        ["id"]=>
        string(2) "40",
        ["count"]=>
        string(1) "4"
    }
}

As you can see I need to add a new key name (count) to my second array and combine values to my first array.

What can I do to output this array combined?

3
  • Not quite sure why that edit was necessary, I've rollbacked it to the previous version since it contained more and better information. Commented Nov 21, 2017 at 11:02
  • Showing var_dump doesn't make any sense as compared to the actual array... Commented Nov 21, 2017 at 11:06
  • 1
    @YashParekh I've accepted your changes before Commented Nov 21, 2017 at 11:09

4 Answers 4

2

Try something like the following. The idea is to iterate on the first array and for each array index add a new key "count" that holds the value contained on the same index of the second array.

$array1 = [];
$array2 = [];

for ($i = 0; $i < count($array1); $i++) {
    $array1[$i]['count'] = $array2[$i];
}
Sign up to request clarification or add additional context in comments.

1 Comment

@SimonOrro No you don't. This code snippet can't produce that warning. Can you show your updated code? 3v4l.org/otrva
1

you can do it like this

$arr1=[["id"=>1],["id"=>40]];
$arr2=[1008,4];
for($i=0;$i<count($arr2);$i++){
  $arr1[$i]["count"] = $arr2[$i];
}

Live demo : https://eval.in/904266

output is

Array
(
    [0] => Array
        (
            [id] => 1
            [count] => 1008
        )

    [1] => Array
        (
            [id] => 40
            [count] => 4
        )

)

Comments

1

Another functional approach (this won't mutate/change the initial arrays):

$arr1 = [['id'=> "1"], ['id'=> "40"]];
$arr2 = ["1008", "4"];

$result = array_map(function($a){
    return array_combine(['id', 'count'], $a);
}, array_map(null, array_column($arr1, 'id'), $arr2));

print_r($result);

The output:

Array
(
    [0] => Array
        (
            [id] => 1
            [count] => 1008
        )

    [1] => Array
        (
            [id] => 40
            [count] => 4
        )
)

Comments

0

Or another approach with recursion:

$arr1=[["id"=>1],["id"=>40]];
$arr2=[1008,4];

foreach ($arr1 as $key=>$value) {
    $result[] = array_merge_recursive($arr1[$key], array ("count" => $arr2[$key]));
}

print_r($result);

And output:

Array
(
    [0] => Array
        (
            [id] => 1
            [count] => 1008
        )

    [1] => Array
        (
            [id] => 40
            [count] => 4
        )

)

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.