3v4l.org

run code in 300+ PHP versions simultaneously
<?php // Test Data $timeVariantSerialized = 'a:3:{s:7:"group_a";a:3:{i:0;a:3:{s:10:"id_variant";i:122;s:4:"name";s:13:"3 hour 30 min";s:4:"time";i:12600;}i:1;a:3:{s:10:"id_variant";i:173;s:4:"name";s:6:"3 hour";s:4:"time";i:10800;}i:2;a:3:{s:10:"id_variant";i:271;s:4:"name";s:6:"1 hour";s:4:"time";i:3600;}}s:7:"group_b";a:2:{i:0;a:3:{s:10:"id_variant";i:107;s:4:"name";s:13:"1 hour 30 min";s:4:"time";i:5400;}i:1;a:3:{s:10:"id_variant";i:321;s:4:"name";s:6:"3 hour";s:4:"time";i:10800;}}s:7:"group_c";a:4:{i:0;a:3:{s:10:"id_variant";i:28;s:4:"name";s:6:"1 hour";s:4:"time";i:3600;}i:1;a:3:{s:10:"id_variant";i:98;s:4:"name";s:6:"3 hour";s:4:"time";i:10800;}i:2;a:3:{s:10:"id_variant";i:157;s:4:"name";s:6:"2 hour";s:4:"time";i:7200;}i:3;a:3:{s:10:"id_variant";i:158;s:4:"name";s:13:"1 hour 30 min";s:4:"time";i:5400;}}}'; // Test Data Array $time_variant = unserialize( $timeVariantSerialized ); $groupsCount = count( $time_variant ); foreach( $time_variant as $groupId => $groupArray ) { foreach( $groupArray as $groupChildId => $groupChildArray ) { $timeCountsArray[$groupChildArray['time']][$groupId] = $groupChildId; } } $result = array(); foreach( $timeCountsArray as $time => $groupIdsArray ) { if( $groupsCount > count( $groupIdsArray ) ) continue; foreach( $groupIdsArray as $groupId => $groupChildId ) { $result[$time_variant[$groupId][$groupChildId]['id_variant']] = $time_variant[$groupId][$groupChildId]; // If you need only one result , uncomment the below break // break; } // If more than 1 time could have variants , comment out below break to get them all break; } ksort( $result ); print_r( $result );
Output for 4.3.0 - 4.3.11, 4.4.0 - 4.4.9, 5.0.0 - 5.0.5, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.37, 5.6.0 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.10, 7.2.0 - 7.2.33, 7.3.12 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.27, 8.2.0 - 8.2.17, 8.3.0 - 8.3.4
Array ( [98] => Array ( [id_variant] => 98 [name] => 3 hour [time] => 10800 ) [173] => Array ( [id_variant] => 173 [name] => 3 hour [time] => 10800 ) [321] => Array ( [id_variant] => 321 [name] => 3 hour [time] => 10800 ) )

preferences:
243.44 ms | 406 KiB | 345 Q