1

I have three arrays first array include ids and employees name and second array have monthly collection with employee ids and third array have daily collection with employee id and daily collection I want to merge these array with ids and name and dcollection and monthly collection but the desired output is not coming here my first array $ids is

Array
(
    [0] => stdClass Object
        (
            [id] => 1
            [name] => Rohit
        )

    [1] => stdClass Object
        (
            [id] => 2
            [name] => Emop1
        )

    [2] => stdClass Object
        (
            [id] => 3
            [name] => Pankaj
        )

    [3] => stdClass Object
        (
            [id] => 4
            [name] => tejpal singh
        )
)

second array $q1 is

Array
(
    [0] => stdClass Object
        (
            [name] => Rohit
            [id] => 1
            [mcollecton] => 100
        )

    [1] => stdClass Object
        (
            [name] => Emop1
            [id] => 2
            [mcollecton] => 1222
        )

)

third array $q2 is

Array
(
    [0] => stdClass Object
        (
            [name] => Rohit
            [id] => 1
            [dcollecton] => 300
        )

    [1] => stdClass Object
        (
            [name] => Emop1
            [id] => 2
            [dcollecton] => 150
        )
)

so far what I have tried

$new_array = array();
foreach($ids as $k) {
   $q1n = array("id"=>$k->id,"name"=>$k->name);
   foreach($q1 as $k1) {
       
       if($k->id==$k1->id){
           $mc = array("mc"=>$k1->mcollecton);    
           array_merge($q1n,$mc);
       }
   }
   
   foreach($q2 as $k1){
       if($k->id==$k1->id){
           $dc = array("dc"=>$k1->dcollecton);
           array_merge($q1n,$dc);
       }
   }
   
    $a = array_merge($q1n,$mc);
    $av = array_merge($q1n,$dc);
    array_push($new_array,$q1n); 
}

but the output is coming as

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => Rohit
        )

    [1] => Array
        (
            [id] => 2
            [name] => Emop1
        )

    [2] => Array
        (
            [id] => 3
            [name] => Pankaj
        )

    [3] => Array
        (
            [id] => 4
            [name] => tejpal singh
        )

)

I want the output be like

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => Rohit
            [mcollection] => 100
            [dcollection] => 300
        )

    [1] => Array
        (
            [id] => 2
            [name] => Emop1
            [mcollection] => 1222
            [dcollection] => 150
        )

    [2] => Array
        (
            [id] => 3
            [name] => Pankaj
            [mcollection] => 0
            [dcollection] => 0
        )

    [3] => Array
        (
            [id] => 4
            [name] => tejpal singh
            [mcollection] => 0
            [dcollection] => 0
        )

)

So I have tried many times but the desired output is not coming . please help me out how to get the desired output.

3
  • Have you taken a look at this Answer? stackoverflow.com/a/13669708/3103434 Commented Jun 21, 2020 at 9:24
  • There are only two arrays Commented Jun 21, 2020 at 9:39
  • You need to perform a nested loop checking for the id's where the id from one array equals the other. At least that's one way to do it. Commented Jun 21, 2020 at 9:54

1 Answer 1

0

It seemed like that answer could be modified, or put in a function that you could call multiple times if needed to combine more than two arrays.

There's probably cleaner ways to handle this with array functions like array_merge or array_walk, but this is the general idea of how I might approach it. I haven't tested this, but maybe it's useful.

foreach($first as $key1 => $value){
    foreach($second as $key2 => $value2){
        // match the ids and check if array key exists on first array
        if($value['id'] === $value2['id'] && empty($first[$key2])){
            $first[$key][$key2] = $value2;
        }               
    }
}

EDIT: Based on the answer you posted vs the question you asked, are you incrementing the collection numbers or just setting them? In other words why use +=? You should also be able to remove array_merge and array_push.

Below is geared more towards what you're trying to do. I haven't tested this either, but if you run into errors, post your code with the errors returned so that it's easier to debug:

foreach($ids as $k)
{
   $thisArray = $newArray[] = array("id"=>$k->id,"name"=>$k->name);

   foreach($q1 as $k1)
   {
       if($k->id == $k1->id && !empty($k1->mcollecton))
       {
            $thisArray['mc'] = $k1->mcollecton;
       }
   }
   
   foreach($q2 as $k2)
   {
       if($k->id == $k2->id && !empty($k2->dcollecton))
       {
            $thisArray['dc'] = $k2->dcollecton;
       }
   }
}

// This should have both new collections fields on all array items
print_r($newArray)
Sign up to request clarification or add additional context in comments.

1 Comment

What error shows up? I just modified my answer to be more specific to what you're trying to do, but if you don't post the errors, it's hard to help.

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.