3v4l.org

run code in 300+ PHP versions simultaneously
<?php $original = Array ( 'id' => Array ( 0 => 1, 1 => 3 ), 'reward' => Array ( 0 => 'Movie', 1 => 'Trip' ), 'cost' => Array ( 0 => 50, 1 => 200 ) ); $output = array(); // Iterate the original array's [id] sub-array foreach ($original['id'] as $idxkey => $newkey) { // Add a sub-array using $newkey to the output array $output[$newkey] = array( // Using the index (not value), retrieve the corresponding reward // value to use as the new array key // and corresponding cost to use as the new subarray value $original['reward'][$idxkey] => $original['cost'][$idxkey] ); } print_r($output);
Output for 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.34, 7.3.0 - 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.19, 8.3.0 - 8.3.7
Array ( [1] => Array ( [Movie] => 50 ) [3] => Array ( [Trip] => 200 ) )

preferences:
245.48 ms | 404 KiB | 339 Q