3v4l.org

run code in 300+ PHP versions simultaneously
<?php header('Content-type: text/plain'); $metaarr = array( array( "post_id" => '34', "meta_key" => "product_shops_0_price", "meta_value"=> "25" ), array( "post_id" => '34', "meta_key" => "product_shops_0_price_old", "meta_value"=> "45" ), array( "post_id" => '34', "meta_key" => "product_shops_1_price", "meta_value"=> "25" ), array( "post_id" => '34', "meta_key" => "product_shops_1_price_old", "meta_value"=> "45" ) ); $map = array(); foreach ($metaarr as $key => $value) { $splarr = preg_match('/^(product_shops_\d*)_(\S*)/', $value['meta_key'], $matches); $product_offset = $matches[1]; $map[$value['post_id']][$product_offset][$value['meta_key']] = $value['meta_value']; } print_r($map);

preferences:
25.19 ms | 406 KiB | 5 Q