0

I have these two arrays:

niceviewA Array (

[0] => Array ( [dow] => Monday [goalamount] => 1000.00 ) 
[1] => Array ( [dow] => Tuesday [goalamount] => 1000.00 ) 
[2] => Array ( [dow] => Wednesday [goalamount] => 1500.00 ) 
[3] => Array ( [dow] => Thursday [goalamount] => 1800.00 ) 
[4] => Array ( [dow] => Friday [goalamount] => 1800.00 ) 
[5] => Array ( [dow] => Saturday [goalamount] => 2000.00 ) 
[6] => Array ( [dow] => Sunday [goalamount] => 1500.00 ) )

and this actualNet Array (

[0] => Array ( [dow] => Friday [NetSales] => 1542.56 [ClosingBarTender] => Bill [OtherBartenders] => Jeremy,Rilee ) 
[1] => Array ( [dow] => Thursday [NetSales] => 1219.89 [ClosingBarTender] => Bill [OtherBartenders] => Vic ) 
[2] => Array ( [dow] => Wednesday [NetSales] => 1019.9 [ClosingBarTender] => Cora [OtherBartenders] => Tory ) 
[3] => Array ( [dow] => Tuesday [NetSales] => 1317.44 [ClosingBarTender] => Bill [OtherBartenders] => Bill ) 
[4] => Array ( [dow] => Monday [NetSales] => 907.85 [ClosingBarTender] => Rilee [OtherBartenders] => Bill ) 
[5] => Array ( [dow] => Sunday [NetSales] => 1954.84 [ClosingBarTender] => Rilee [OtherBartenders] => Ginger ) 
[6] => Array ( [dow] => Saturday [NetSales] => 2496.28 [ClosingBarTender] => Rilee [OtherBartenders] => LJ,Ginger ) )

I used array_replace_recursive, but that just joined them on index, I need to join them on the 'dow' element, so it would look like this:

actualNet Array (

[0] => Array ( [dow] => Friday [NetSales] => 1542.56 [ClosingBarTender] => Bill [OtherBartenders] => Jeremy,Rilee [goalamount] => 1800.00) 
[1] => Array ( [dow] => Thursday [NetSales] => 1219.89 [ClosingBarTender] => Bill [OtherBartenders] => Vic [goalamount] => 1800.00) 
[2] => Array ( [dow] => Wednesday [NetSales] => 1019.9 [ClosingBarTender] => Cora [OtherBartenders] => Tory [goalamount] => 1500.00) 
[3] => Array ( [dow] => Tuesday [NetSales] => 1317.44 [ClosingBarTender] => Bill [OtherBartenders] => Bill [goalamount] => 1000.00) 
[4] => Array ( [dow] => Monday [NetSales] => 907.85 [ClosingBarTender] => Rilee [OtherBartenders] => Bill [goalamount] => 1000.00) 
[5] => Array ( [dow] => Sunday [NetSales] => 1954.84 [ClosingBarTender] => Rilee [OtherBartenders] => Ginger[goalamount] => 1500.00 ) 
[6] => Array ( [dow] => Saturday [NetSales] => 2496.28 [ClosingBarTender] => Rilee [OtherBartenders] => LJ,Ginger [goalamount] => 20000.00) )

Not like this: niceview2Array (

[0] => Array ( [dow] => Friday [goalamount] => 1000.00 [NetSales] => 1542.56 [ClosingBarTender] => Bill [OtherBartenders] => Jeremy,Rilee ) 
[1] => Array ( [dow] => Thursday [goalamount] => 1000.00 [NetSales] => 1219.89 [ClosingBarTender] => Bill [OtherBartenders] => Vic ) 
[2] => Array ( [dow] => Wednesday [goalamount] => 1500.00 [NetSales] => 1019.9 [ClosingBarTender] => Cora [OtherBartenders] => Tory ) 
[3] => Array ( [dow] => Tuesday [goalamount] => 1800.00 [NetSales] => 1317.44 [ClosingBarTender] => Bill [OtherBartenders] => Bill ) 
[4] => Array ( [dow] => Monday [goalamount] => 1800.00 [NetSales] => 907.85 [ClosingBarTender] => Rilee [OtherBartenders] => Bill ) 
[5] => Array ( [dow] => Sunday [goalamount] => 2000.00 [NetSales] => 1954.84 [ClosingBarTender] => Rilee [OtherBartenders] => Ginger ) 
[6] => Array ( [dow] => Saturday [goalamount] => 1500.00 [NetSales] => 2496.28 [ClosingBarTender] => Rilee [OtherBartenders] => LJ,Ginger ) ) 

I have tried to iterate through the arrays, but I can’t get a match on ‘dow’. I’m so frustrated I have come to stackoverflow and hope some one can help! Thanks so much!

4
  • Show us the code you used to itereate through the arrays Commented Jul 28, 2018 at 13:25
  • try stackoverflow.com/questions/49831781/… Commented Jul 28, 2018 at 13:27
  • That isn't the same @MohdHasan. The arrays I am now merging based on the answer below works. I have an array in an array and was not doing it correctly. Thanks for all of your replies. About the code that didn't work, I would have to go back a few versions of my code to find it. Commented Jul 28, 2018 at 14:16
  • I would be using a couple of array_column() calls to prepare the data for an array_replace_recursive() call as I demonstrate at the duplicate page. If you have some pressing urge to micro-optimize and require nested foreach traversal, then you can use Shiv's snippet more performantly with a break in the inner loop and without the array_merge() or array_values() calls. 3v4l.org/mSkhQ Commented Jan 12, 2020 at 20:48

1 Answer 1

0
$first = [ ['dow' => 'Monday' , 'goalamount' => 1000.00 ],
        ['dow' => 'Tuesday' , 'goalamount' => 1500.00 ],
        ['dow' => 'Wednesday' , 'goalamount' => 1800.00 ],
        ['dow' => 'Thursday' , 'goalamount' => 1800.00 ],
        ['dow' => 'Friday' , 'goalamount' => 2000.00 ],
        ['dow' => 'Saturday' , 'goalamount' => 1500.00 ],
        ['dow' => 'Sunday' , 'goalamount' => 1500.00 ],
    ];
    $second = [ [ 'dow' => 'Friday', 'NetSales' => 1542.56, 'ClosingBarTender' => 'Bill', 'OtherBartenders' => 'Jeremy,Rilee'],
        [ 'dow' => 'Thursday', 'NetSales' => 1219.89, 'ClosingBarTender' => 'Bill', 'OtherBartenders' => 'Vic'],
        [ 'dow' => 'Wednesday', 'NetSales' => 1019.9, 'ClosingBarTender' => 'Cora', 'OtherBartenders' => 'Tory'],
        [ 'dow' => 'Tuesday', 'NetSales' => 1317.44, 'ClosingBarTender' => 'Bill', 'OtherBartenders' => 'Bill'],
        [ 'dow' => 'Monday', 'NetSales' => 907.85, 'ClosingBarTender' => 'Rilee', 'OtherBartenders' => 'Bill'],
        [ 'dow' => 'Sunday', 'NetSales' => 1954.84, 'ClosingBarTender' => 'Rilee', 'OtherBartenders' => 'Ginger'],
        [ 'dow' => 'Saturday', 'NetSales' => 2496.28, 'ClosingBarTender' => 'Rilee', 'OtherBartenders' => 'JLJ,Ginger'],
    ];
    $result = [];
     foreach( $second as $key => $value) {
         foreach($first as $v ) {
            if($value['dow'] == $v['dow']) {
                $result[$value['dow']] = array_merge($value, [ 'goalamount'=> $v['goalamount'] ] ) ;
            }
       }
   }
        print_r(array_values($result));

Output:

 Array
(
    [0] => Array
        (
            [dow] => Friday
            [NetSales] => 1542.56
            [ClosingBarTender] => Bill
            [OtherBartenders] => Jeremy,Rilee
            [goalamount] => 2000
        )

    [1] => Array
        (
            [dow] => Thursday
            [NetSales] => 1219.89
            [ClosingBarTender] => Bill
            [OtherBartenders] => Vic
            [goalamount] => 1800
        )

    [2] => Array
        (
            [dow] => Wednesday
            [NetSales] => 1019.9
            [ClosingBarTender] => Cora
            [OtherBartenders] => Tory
            [goalamount] => 1800
        )

    [3] => Array
        (
            [dow] => Tuesday
            [NetSales] => 1317.44
            [ClosingBarTender] => Bill
            [OtherBartenders] => Bill
            [goalamount] => 1500
        )

    [4] => Array
        (
            [dow] => Monday
            [NetSales] => 907.85
            [ClosingBarTender] => Rilee
            [OtherBartenders] => Bill
            [goalamount] => 1000
        )

    [5] => Array
        (
            [dow] => Sunday
            [NetSales] => 1954.84
            [ClosingBarTender] => Rilee
            [OtherBartenders] => Ginger
            [goalamount] => 1500
        )

    [6] => Array
        (
            [dow] => Saturday
            [NetSales] => 2496.28
            [ClosingBarTender] => Rilee
            [OtherBartenders] => JLJ,Ginger
            [goalamount] => 1500
        )

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

2 Comments

thanks so much! I think I was missing the array_merge when I iterated through the array.
I did vote the answer, alas I don't have the reputation to have it publicly viewed.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.