I'm trying to access next key-value pair of associate array in php to check whether next key-value pair is same or not.
foreach($array as $key => $value){
$b = $value['date'];
$c = ($key+1)['date']; // As ($key+1) is integer value not an array
if($b == $c){
statement
}
}
However, This approach is throwing below which seems to be logical.
ErrorException: Trying to access array offset on value of type int
Is there any way i could find next element inside foreach loop in associate array.
array (
0 =>
array (
'date' => "2019-03-31",
'a' => '1',
'b' => '1',
),
1 =>
array (
'date' => "2019-04-02",
'a' => '1',
'b' => '1',
),
2 =>
array (
'date' => "2019-04-02",
'a' => '2',
'b' => '1',
)
)
($key+1)is an integer value, so($key+1)['date']will complain. Try$array[$key + 1][$date]and be careful for the last iteration.