0

I have two arrays of arrays. here is the demonstration of the two arrays for the sake of simplicity: The $rows array:

Array ( [0] => Array ( [Badge] => Adrian [Denied] => 2 [Correct] => 9 ) 
        [1] => Array ( [Badge] => Adriann [Denied] => 3 [Correct] => 6 ) 
        [2] => Array ( [Badge] => newAd [Denied] => 0 [Correct] => 4 ) ) 

AND the $overrides_array:

Array ( [0] => Array ( [Badge] => Adrian [Override] => 2 ) 
        [1] => Array ( [Badge] => newAd [Override] => 1 ) 
        [2] => Array ( [Badge] => AntonRapid [Override] => 1 ) )

Now, I want to merge these two arrays into one in a way that I end up with the followwing:

Array ( [0] => Array ( [Badge] => Adrian [Denied] => 2 [Correct] => 9 [Override] => 2 ) 
        [1] => Array ( [Badge] => Adriann [Denied] => 3 [Correct] => 6 [Override] => 0 ) 
        [2] => Array ( [Badge] => newAd [Denied] => 0 [Correct] => 4 [Override] => 1 ) 
        [2] => Array ( [Badge] => AntonRapid [Denied] => 0 [Correct] => 0 [Override] => 1 )
      )

So far I have come up with the following ugly code but it wouldn't work in case of some of the Badges:

$result_array = array();
    $counter = 0;
    foreach ($rows as $row){
        //$counter = 0;
        if(count($override_array) > 0){
            foreach($override_array as $override){
              if($row['Badge'] == $override['Badge'] ){

                  $row = $this->array_push_assoc($row, 'Override', $override['Override']);
                  unset($override_array[$counter]);
                  $counter++;
              }


          }
        }
        else $row=$this->array_push_assoc($row, 'Override', '0');

        $result_array[]=$row;

    }
    $roww = array();
    //print_r($override_array);
    if(count($override_array) > 0){
        foreach ($override_array as $override){
            $roww = $this->array_push_assoc($roww, 'Override', $override['Override']);
            $roww = $this->array_push_assoc($roww, 'badge', $override['Badge']);
            $roww = $this->array_push_assoc($roww, 'Correct', '0');
            $roww = $this->array_push_assoc($roww, 'Denied', '0');
            $result_array[]=$roww;
        }
    }

2 Answers 2

1

If I undestood You right, You could use $result = array_replace_recursive($rows, $overrides); in this case.

It recursively merges not duplicated 'key-paths' and replaces values of duplicated ones.

As in an example in php manual [1].

[1] http://www.php.net/manual/en/function.array-replace-recursive.php

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

1 Comment

it works but it wouldn't give me an array when a badge exists in overrides bit not in rows. in this case both Correct and Denied must become 0
0

Use array merge

$result = array_merge($arr1,$arr2)

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.