3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array1 = array( 0 => array( 2 => array( "type" => 1, "score" => 10, ), ), 1 => array( 2 => array( "type" => 2, "score" => 5, ), ), 2 => array( 2 => array( "type" => 3, "score" => 4, ), ), 3 => array( 2 => array( "type" => 4, "score" => 60, ), ), 4 => array( 1 => array( "type" => 1, "score" => 5, ), ), ); $array2 = array(0 => 2, 1 => 1); $array3 = array( 2 => array( "score" => 79 ), 1 => array( "score" => 5 ), ); foreach( $array1 as $array1x ){ $key=key($array1x); if( in_array($key, $array2) ){ $this_score = $array1x[$key]['score']; $cum_score = isset($newarray[$key]['score']) ? $newarray[$key]['score'] : 0; $newarray[$key]['score'] = $cum_score + $this_score; } } print_r($newarray);
Output for 7.3.13 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
Array ( [2] => Array ( [score] => 79 ) [1] => Array ( [score] => 5 ) )
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 Array ( [2] => Array ( [score] => 79 ) [1] => Array ( [score] => 5 ) )

preferences:
152.27 ms | 402 KiB | 143 Q