【发布时间】:2014-01-21 15:09:18
【问题描述】:
我在 tutplus 上关注 this tutorial,发现了这个代码 sn-p:
//check if the action exists in the controller. if not, throw an exception.
if( method_exists($controller, $action) === false ) {
throw new Exception('Action is invalid.');
}
//execute the action
$result['data'] = $controller->$action();
$result['success'] = true;
} catch( Exception $e ) {
//catch any exceptions and report the problem
$result = array();
$result['success'] = false;
$result['errormsg'] = $e->getMessage();
}
//echo the result of the API call
echo json_encode($result);
exit();
我是 PHP 初学者,我想知道 result 是否是关联数组?有人可以确认一下吗?如何区分关联数组和非关联数组?
【问题讨论】:
-
是的。因为您为数组中的每个元素提供了一个键名。
-
它是一个关联数组。它将键:“sucess”、“errormsg”与值相关联。
-
传统的索引数组使用零基值而不是键值对。