3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Unit { /** * checkMapzone * 检测mapzone * @param mixed $params * @static * @access public * @return void */ public static function checkMapzone($params) { $allRegion = array( array( 'provinceId' => 100000, 'cityIds' => array(), ), ); $mapzone = $params['mapzone']; $mapzone = self::__checkMapzoneFormat($mapzone); $provinceIds = Tools\Common::array_column($mapzone, 'provinceId'); if (in_array(100000, $provinceIds)) { $params['mapzone'] = \App::jsonEncode($allRegion); return $params; } $idToCityMap = \App::getConf('city')['idToCityMap']; foreach ($mapzone as $area) { if (!array_key_exists($area['provinceId'], $idToCityMap)) { throw new UserException("无效的省份"); } foreach ($area['cityIds'] as $cityId) { if (!array_key_exists($cityId, $idToCityMap[$area['provinceId']]['value'])) { throw new UserException("无效城市"); } } } $params['mapzone'] = \App::jsonEncode($mapzone); return $params; } /** * __checkMapzoneFormat * 检测mapzone格式是否正确 * @param mixed $mapzone * @static * @access private * @return void */ public static function __checkMapzoneFormat($mapzone) { $dec = json_decode($mapzone); if (!is_array($dec)) { throw new \Exception("投放地域格式错误"); } $mapzone = json_decode($mapzone, true); foreach ($mapzone as $key => $area) { if (!isset($area['provinceId']) || preg_match(GeneralMatch::POSITIVE_INTEGER, $area['provinceId']) != 1) { throw new \Exception("mapzone格式不正确(provinceId)"); } $mapzone[$key]['provinceId'] = intval($area['provinceId']); if (!isset($area['cityIds']) || !is_array($area['cityIds'])) { throw new \Exception("mapzone格式不正确(cityIds)"); } foreach ($area['cityIds'] as $k => $cityId) { if (preg_match(GeneralMatch::POSITIVE_INTEGER, $cityId) != 1) { throw new \Exception("mapzone格式不正确(cityIds)"); } $mapzone[$key]['cityIds'][$k] = intval($cityId); } } return json_encode($mapzone); } } $params['mapzone'] = '[{"provinceId":"19","cityIds":[59]}]'; $params['mapzone'] = Unit::__checkMapzoneFormat($params); echo $params['mapzone'];
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
Fatal error: Uncaught TypeError: json_decode(): Argument #1 ($json) must be of type string, array given in /in/iJbJM:54 Stack trace: #0 /in/iJbJM(54): json_decode(Array) #1 /in/iJbJM(83): Unit::__checkMapzoneFormat(Array) #2 {main} thrown in /in/iJbJM on line 54
Process exited with code 255.
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 Fatal error: Uncaught TypeError: json_decode(): Argument #1 ($json) must be of type string, array given in /in/iJbJM:54 Stack trace: #0 /in/iJbJM(54): json_decode(Array) #1 /in/iJbJM(83): Unit::__checkMapzoneFormat(Array) #2 {main} thrown in /in/iJbJM on line 54
Process exited with code 255.
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.25, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33
Warning: json_decode() expects parameter 1 to be string, array given in /in/iJbJM on line 54 Fatal error: Uncaught Exception: 投放地域格式错误 in /in/iJbJM:56 Stack trace: #0 /in/iJbJM(83): Unit::__checkMapzoneFormat(Array) #1 {main} thrown in /in/iJbJM on line 56
Process exited with code 255.
Output for 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.28
Warning: json_decode() expects parameter 1 to be string, array given in /in/iJbJM on line 54 Fatal error: Uncaught exception 'Exception' with message '投放地域格式错误' in /in/iJbJM:56 Stack trace: #0 /in/iJbJM(83): Unit::__checkMapzoneFormat(Array) #1 {main} thrown in /in/iJbJM on line 56
Process exited with code 255.

preferences:
202.73 ms | 402 KiB | 283 Q