3v4l.org

run code in 300+ PHP versions simultaneously
<?php // 新しく追加する配列 $new_post_info_wrapper = [ [ 'insert_name' => 'julia', 'post_info_arr' => [ ['id'=>4,'area'=>'saitama','post_type'=>'d'] ], ], [ 'insert_name' => 'ally', 'post_info_arr' => [ ['id'=>5,'area'=>'gunma','post_type'=>'e'] ], ], ]; // infinityで、かつ同じinsert_nameの場合に連結したい $result = get_result_post_info_wrapper( 'infinity', $new_post_info_wrapper ); var_dump( $result ); // 最終的な配列 function get_result_post_info_wrapper( $process, $new_post_info_wrapper ){ $result = []; // データベースから現在値を取得 $leave_post_info_wrapper = get_leave_post_info_wrapper(); // 現在値がなければ if( empty($leave_post_info_wrapper) ){ $result = ext_post_info_wrapper_for_leave( $new_post_info_wrapper ); } // 現在値があれば else{ foreach( $leave_post_info_wrapper as &$leave_wrapper ){ $leave_insert_name = $leave_wrapper['insert_name']; $leave_post_info_arr = $leave_wrapper['post_info_arr']; foreach( $new_post_info_wrapper as $new_wrapper ){ $new_insert_name = $new_wrapper['insert_name']; $new_post_info_arr = $new_wrapper['post_info_arr']; // 必要な情報だけ抽出 foreach( $new_post_info_arr as &$new_post_info ){ $exted_post_info = ext_post_info( $new_post_info ); } // insert_nameが同じものがある場合 if( $new_insert_name == $leave_insert_name ){ // 無限ロードなら追加 if( $process == 'infinity' ){ if( $leave_wrapper['insert_name'] == $new_insert_name ){ $leave_wrapper['post_info_arr'][] = $exted_post_info; $result = $leave_post_info_wrapper; } } } // 新しい insert_name の場合 else{ $result = ext_post_info_wrapper( $new_post_info_wrapper ); } } } } return $result; } // データベースにある配列 function get_leave_post_info_wrapper(){ return [ [ 'insert_name' => 'julia', 'post_info_arr' => [ ['id'=>1,'post_type'=>'a'] ], ], [ 'insert_name' => 'ally', 'post_info_arr' => [ ['id'=>2,'area'=>'kanagawa','post_type'=>'b'], ['id'=>3,'area'=>'chiba','post_type'=>'c'] ], ], ]; } // post_info_wrapper から必要な値を抽出 function ext_post_info_wrapper( $post_info_wrapper ){ foreach( $post_info_wrapper as &$wrapper ){ $new_post_info_arr = $wrapper['post_info_arr']; foreach( $new_post_info_arr as &$new_post_info ){ $wrapper['post_info_arr'] = []; $wrapper['post_info_arr'][] = ext_post_info( $new_post_info ); } } return $post_info_wrapper; } // post_info から必要な値を抽出 function ext_post_info( $post_info ){ $exted_post_info = []; $post_type = $post_info['post_type']; // 抽出したい情報を指定 if( $post_type == 'a' ){ $key_arr = ['post_type','id']; } else{ $key_arr = ['post_type','area']; } // 抽出 foreach( $key_arr as $key ){ $exted_post_info[$key] = $post_info[$key]; } return $exted_post_info; }
Output for 7.2.0 - 7.2.33, 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.4, 8.3.6 - 8.3.7
array(2) { [0]=> array(2) { ["insert_name"]=> string(5) "julia" ["post_info_arr"]=> array(2) { [0]=> array(2) { ["id"]=> int(1) ["post_type"]=> string(1) "a" } [1]=> array(2) { ["post_type"]=> string(1) "d" ["area"]=> string(7) "saitama" } } } [1]=> array(2) { ["insert_name"]=> string(4) "ally" ["post_info_arr"]=> array(3) { [0]=> array(3) { ["id"]=> int(2) ["area"]=> string(8) "kanagawa" ["post_type"]=> string(1) "b" } [1]=> array(3) { ["id"]=> int(3) ["area"]=> string(5) "chiba" ["post_type"]=> string(1) "c" } [2]=> array(2) { ["post_type"]=> string(1) "e" ["area"]=> string(5) "gunma" } } } }
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) { [0]=> array(2) { ["insert_name"]=> string(5) "julia" ["post_info_arr"]=> array(2) { [0]=> array(2) { ["id"]=> int(1) ["post_type"]=> string(1) "a" } [1]=> array(2) { ["post_type"]=> string(1) "d" ["area"]=> string(7) "saitama" } } } [1]=> array(2) { ["insert_name"]=> string(4) "ally" ["post_info_arr"]=> array(3) { [0]=> array(3) { ["id"]=> int(2) ["area"]=> string(8) "kanagawa" ["post_type"]=> string(1) "b" } [1]=> array(3) { ["id"]=> int(3) ["area"]=> string(5) "chiba" ["post_type"]=> string(1) "c" } [2]=> array(2) { ["post_type"]=> string(1) "e" ["area"]=> string(5) "gunma" } } } }

preferences:
181.08 ms | 404 KiB | 178 Q