3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?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'];
based on oEMds
Output for 7.0.0 - 7.2.0
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.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.