Mat*_*hew 2 php arrays indexing associative-array
我有一个方法,fetch_widgets
从(MySQL)数据库中获取小部件.我传递了一个$options
数组,所以我可以有选择地添加WHERE
和JOIN
子句,添加/删除列等.
例如:
$options = array();
$options[] = 'include_disabled';
$options[] = 'include_tag_ids';
$options['start_date'] = '2011-01-01';
$options['end_date'] = '2011-01-31';
Run Code Online (Sandbox Code Playgroud)
在fetch_widgets
我使用以下任一方法检查选项:
if(array_key_exists('start_date',$options)) { ... }
Run Code Online (Sandbox Code Playgroud)
要么:
if(in_array('include_tag_ids',$options)) { ... }
Run Code Online (Sandbox Code Playgroud)
取决于选项是否被激活只是存在(例如include_disabled
)或具有键和值(例如end_date
).
我遇到了困难,因为in_array
当$options
数组包含键控和非键控值时,我得到了奇怪的结果.任何人都可以对此有所了解吗?
小智 5
如果需要一致的行为,请不要混合/匹配键控和非键控阵列.
相反,做这样的事情:
$options = array();
$options['include_disabled'] = true;
$options['include_tag_ids'] = true;
$options['start_date'] = '2011-01-01';
$options['end_date'] = '2011-01-31';
Run Code Online (Sandbox Code Playgroud)