我浏览了两者的手册页数组键 and 数组值。他们都没有提及是否遵守原始数组的元素顺序。他们所承诺的只是返回原始数组中的所有键或值。但是我们能绝对确定元素的顺序也与原始数组的顺序完全相同吗?不管是什么数组?
我问这是因为我有这个:
$record = array('name' => 'Lisa', 'age' => 16, 'gender' => 'female');
$fields = array_keys($record);
$values = array_values($record);
$sql = "INSERT INTO {$this -> table} (".implode(', ', $fields).") VALUES (".implode(', ', array_fill(0, count($fields), '?')).")";
$sth = $this -> dbh -> prepare($sql);
$sth -> execute($values);
虽然我可以使用命名参数,但它会花费更多的编码,所以我更喜欢这种方式,它要求 $fields 和 $values 的元素位于相应的对中,最好它们的顺序与原始数组 $record 的顺序完全相同。
任何想法?
是的,他们是。事实上,手册中没有提到这一点,所以我指的是内部实现。这是示例array_keys():
/* Go through input array and add keys to the return array */
zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(input), &pos);
while (zend_hash_get_current_data_ex(Z_ARRVAL_P(input), (void **)&entry, &pos) == SUCCESS) {
if (search_value != NULL) {
is_equal_func(&res, search_value, *entry TSRMLS_CC);
add_key = zval_is_true(&res);
}
if (add_key) {
MAKE_STD_ZVAL(new_val);
zend_hash_get_current_key_zval_ex(Z_ARRVAL_P(input), new_val, &pos);
zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &new_val, sizeof(zval *), NULL);
}
zend_hash_move_forward_ex(Z_ARRVAL_P(input), &pos);
}
-嗯,是的,上面的代码是用 C 语言编写的,但它确实显示了函数内部的内部逻辑。我认为lxr对于搜索非常友好 - 所以我省略了诸如宏定义之类的东西(它们不在这个问题中) - 但你可以更深入地调查全貌。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)