Compare two multidimensional array?


#1

Good day. Need some help I fixed the code

 $new  =[ 'time' => 0 , 'time2' => 0 ];
 $a  =  [ 
0 => [ 'a' => '2018-01-01', 'time' => 2 ],
1 => [ 'a' => '2018-01-02', 'time' => 1 ],
2 => [ 'a' => '2018-01-03' , 'time' => 1],
3 => [ 'a' => '2018-01-04'  , 'time' => 1],
 ];
 $b=  [ 
0 => [ 'a' => '2018-01-01' ],
1 => [ 'a' => '2018-01-02' ]
 ];

foreach($a as $a1) {
    foreach($b as b2 ) {
      if($b2['date'] == $a2['date'] ) {
           $new['time'] += $a1['time'];
      } else {
           $new['time2'] += $a1['time'];
       }
}
}

Expected out put.

Time: 3
Time2: 2
But got unexpected results.


#2

you try to access arrays as if they were objects, which obviously does not work.


#3

Hi @tabaldodennis and welcome to the forum.

Try adding echo '<br>'; var_dump($valToDisplay); after each for loop to display the values inside the loop.


#4

I try it already but i got incorrect value at time2: 5 value instead of 3.


#5

enable error reporting to get the errors shown, then it should be an easy fix.

And please post correct code, what you have posted doesn't even run.


#6

I fixed it.

$new =[ 'time' => 0 , 'time2' => 0 ];
$a = [ 
0 => [ 'a' => '2018-01-01', 'time' => 2 ],
1 => [ 'a' => '2018-01-02', 'time' => 1 ],
2 => [ 'a' => '2018-01-03' , 'time' => 1],
3 => [ 'a' => '2018-01-04' , 'time' => 1]
];
$ = [ 
0 => [ 'a' => '2018-01-01' ],
1 => [ 'a' => '2018-01-02' ]
];

foreach($a as $a1) {
     foreach($b as b2 ) {

       if($a1['time'] === $a1['time'] ) {
           $new['time'] +=$a1['time'];
      } else {
             $new['time2'] +=$a1['time'];
     }
  }
} var_dump($new);

#7

I still don't see how that code actually executes with the various typos and undefined variables. And this line seems to be a little strange:

if($a1['time'] === $a1['time'] ) {

How is this expected? That is, what are you trying to do with the code?


#8

Try this instead because there are still script errors:

<?php 

// NEXT LINE ONLY FOR PHP7 
declare(strict_types=1);

ini_set('display_errors', 'true');
ini_set('display_startup_errors', 'true');
error_reporting(-1);

echo '<pre>';

$new =[ 'time' => 0 , 'time2' => 0 ];
print_r($new);

$a = [ 
0 => [ 'a' => '2018-01-01', 'time' => 2 ],
1 => [ 'a' => '2018-01-02', 'time' => 1 ],
2 => [ 'a' => '2018-01-03' , 'time' => 1],
3 => [ 'a' => '2018-01-04' , 'time' => 1]
];
$ = [ 
0 => [ 'a' => '2018-01-01' ],
1 => [ 'a' => '2018-01-02' ]
];

foreach($a as $a1) {
     foreach($b as b2 ) {

       if($a1['time'] === $a1['time'] ) {
           $new['time'] +=$a1['time'];
      } else {
             $new['time2'] +=$a1['time'];
     }
  }
}

var_dump($new);

echo '</pre>';

SCRIPT ERROR

Parse error: syntax error, unexpected '=', expecting variable (T_VARIABLE) or '{' or '$' in /home/john/www/sp.php on line 24


#9

good day sorry for the wrong code this new one.

$new = [ 'time' => 0, 'time2' => 0 ];
$a = [ 
0 => [ 'a' => '2018-01-01', 'time' => 2 ],
1 => [ 'a' => '2018-01-02', 'time' => 1 ],
2 => [ 'a' => '2018-01-03' , 'time' => 1],
3 => [ 'a' => '2018-01-04' , 'time' => 1]
];
$b = [ 
0 => [ 'a' => '2018-01-01' ],
1 => [ 'a' => '2018-01-02' ]
];

foreach($a as $a1) {
     foreach($b as $b1 ) {

       if($a1['a'] == $b1['a'] ) {
           $new['time'] += $a1['time'];
       }  else { 
        $new['time2'] += $a1['time'];         
     }
  }
}

print_r($new);

the result is Array ( [time] => 3 [time2] => 7 )

whats wrong with the code and how can I get

time : 3 time2 : 2


#10

Try this and fill in the elseif(...) condition:

foreach($a as $a1)
{
	foreach($b as $b1 )
	{
  	if($a1['a'] === $b1['a'] )
  	{
    	echo '<br> First Match ==> ',
    	$new['time'] += $a1['time'];
    }elseif( 0 ){ 
    	echo '<br> Second Match ==>>',
    	$new['time2'] += $a1['time'];         
    }else{ 
    	echo '<br>NO Match';
    }
  }
}

Outout:

First Match ==> 2
NO Match
NO Match
First Match ==> 3
NO Match
NO Match
NO Match
NO Match

$new = Array
(
[time] => 3
[time2] => 0
)

Desired result ==> time : 3 time2 : 2


#11

the logic.

currently your condition is executed 8 times (count $a x count $b). of these 8 executions, there are 2 desired matches (for time), everything else (in total 6 additions) goes into time2, hence the sum of 7.

what you likely want is the difference between the total sum and the calculated sum of time.


#12

is there any solutions?


#13

don't use nested loops, but filter your array before summing up.


#14

array_filter?


#15

@Dormilich thanks problem solve.


#16

For the benefit of other users who may experience similar problems, please share your solution.


#17

I would have used array_uintersect() and array_sum().


#18

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.