3v4l.org

run code in 300+ PHP versions simultaneously
<?php // DBからSQLでソートするためにセットされてるダミー日付 define( 'DATE_DAMMY', '1970-01-01 00:00:00' ); // DBからとってきた生レコード $content_row = [ // コメントの情報 'content_id' => 1 ,'content_content' => 'かわいいですね' ,'content_kind' => 'comment' ,'date_create' => '2020-02-01 00:00:01' // コメントが投稿された日 ,'date_his_create' => '1970-01-01 00:00:00' // 自分が投稿したらここも入る ,'date_vote_up' => '2020-02-01 00:00:02' // 自分がコメントを高評価した日 ,'date_clip' => '1970-01-01 00:00:00' // 自分がコメントを保存した日 // 掲示板の情報 ,'parent_content_id' => 10 ,'parent_title' => '猫の掲示板' ,'parent_content_kind' => 'thread' ,'parent_date_favorite' => '1970-01-01 00:00:00' // 自分が掲示板へいいねした日 // コメントの投稿者の情報 ,'author_content_id' => 100 ,'author_content_content' => '花子' ,'author_date_follow' => '2020-01-01 00:00:00' // 自分が投稿者をフォローした日 ]; // 生レコードである $content_row を $content_info へ調整 $content_info = assort($content_row); var_dump($content_info); function assort( $content_row ){ $content_info = []; foreach( $content_row as $k => $v ){ // 掲示板の情報をまとめる if( str_exist($k,'parent') ){ $key = str_replace('parent_','',$k); $content_info['parent_info'][$key] = $v; } // 投稿者の情報をまとめる elseif( str_exist($k,'author') ){ $key = str_replace('author_','',$k); $content_info['author_info'][$key] = $v; $content_info['author_info']['service_name'] = 'mysite'; $content_info['author_info']['kind'] = 'user'; } // コメントの情報 else{ // 日付 if( str_exist($k,'date') ){ // ダミー日付は入れない if( $v == DATE_DAMMY ) continue; // そのまま入れる $content_info[$k] = $v; // jsでソートするときキーが不定だと処理が遅いのでここで date_latest としてソートキーを統一しておく if( !isset($content_info['date_latest']) ){ // まずこの if でなんでもいいので最新日としてセットして、次の else で新しいものに更新していく $content_info['date_latest'] = $v; }else{ // 新しいものに更新していく $date_latest = ( $v > $content_info['date_latest'] ) ? $v : $content_info['date_latest']; $content_info['date_latest'] = $date_latest; } // 判定系はDBでは 0|1 になってるので真偽値にしておく }elseif( str_exist($k,'is_') ){ $content_info[$k] = $v == 1 ? true : false; // 他 }else{ // そのまま入れる $content_info[$k] = $v; } } } return $content_info; } // strpos の代わり function str_exist( $target, $str ){ $is = false; // 配列渡したらエラー if( is_array($target) || is_array($str) ){ error_log('str_exist() : $target - $str : ' . json_encode($target) . ' - ' . json_encode($str) ); } // 普通の strpos を実行 if(strpos($target,$str) !== false){ $is = true; } return $is; }
Output for 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.32, 8.2.0 - 8.2.28, 8.3.0 - 8.3.19, 8.4.1 - 8.4.5
array(8) { ["content_id"]=> int(1) ["content_content"]=> string(21) "かわいいですね" ["content_kind"]=> string(7) "comment" ["date_create"]=> string(19) "2020-02-01 00:00:01" ["date_latest"]=> string(19) "2020-02-01 00:00:02" ["date_vote_up"]=> string(19) "2020-02-01 00:00:02" ["parent_info"]=> array(4) { ["content_id"]=> int(10) ["title"]=> string(15) "猫の掲示板" ["content_kind"]=> string(6) "thread" ["date_favorite"]=> string(19) "1970-01-01 00:00:00" } ["author_info"]=> array(5) { ["content_id"]=> int(100) ["service_name"]=> string(6) "mysite" ["kind"]=> string(4) "user" ["content_content"]=> string(6) "花子" ["date_follow"]=> string(19) "2020-01-01 00:00:00" } }

preferences:
157.91 ms | 409 KiB | 5 Q