You can use PHP's advanced array destructuring syntax, available from PHP 7.1:
['id_piatto' => $a[], 'id_portata' => $a[], 'id_dieta' => $a[]] = $item;
['id_piatto' => $b[], 'id_portata' => $b[], 'id_dieta' => $b[]] = $riga;
$item['quantita'] += ($a === $b) ? $riga['quantita'] : 0;
I think this looks nice and readable, but it really shines when the input arrays have different keys and/or nested structure, I'll give you an example:
$one = [
'foo' => 1,
'bar' => [1,2,3],
'baz' => ['baz_id' => 4]
];
$two = [
'foo' => 1,
'bar' => [5,5,3],
'boo_id' => 4
];
['foo' => $a[], 'bar' => [2 => $a[]], 'baz' => ['baz_id' => $a[]]] = $one;
['foo' => $b[], 'bar' => [2 => $b[]], 'boo_id' => $b[]] = $two;
var_dump($a === $b); // true
Note how I used a numeric index to access the third value in the 'bar' sub-array. Finally, this is much more flexible than the custom function approach in some of the other answers. For detailed info, Check out this blog post about array destructuring in PHP.
Addendum
If it's all about nice looking code and readability, is this really better than the standard approach (maybe structured with some linebreaks)?
if(
$one['foo'] === $two['foo']
&& $one['bar'][2] === $two['bar'][2]
&& $one['baz']['baz_id'] === $two['boo_id']
) {
// Do something...
}
Apart from the stylistic viewpoint, the standard approach has still two big advantages:
One could easily use an individual comparison operator for each expression
One could implement more advanced boolean logic
One the other hand, the destructuring approach will generate 2 "normalized" subsets of your input arrays on the fly, which could be useful later...
if($a === $b) {
// Do something
} else {
return [
'error_msg' => "Inputs don't match",
'context' => [
'expected' => $a,
'actual' => $b
]
];
}