被责令元素在同一阵列被array_keys()和array_values​​()拆分后? [英] Is elements order the same after array being split by array_keys() and array_values()?

查看:88
本文介绍了被责令元素在同一阵列被array_keys()和array_values​​()拆分后?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过这两个 array_keys array_values​​手册页看了。他们都不说,他们是否兑现了当初的数组元素的顺序什么。所有他们承诺是,他们将返回所有键或值从原来的数组。但是,我们可以绝对确保元素的顺序也将是完全一样的原始数组的?不管它是什么阵列?

I looked through the manual pages of both array_keys and array_values. Neither of them said anything about whether they honor the elements order of the original array. All they promise is they will return all the keys or values from the original array. But can we be absolutely sure that the order of the elements will also be exactly the same as that of the original array? No matter what array it is?

请问这是因为我有这样的:

I ask this is because I have this:

$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);

虽然我可以使用命名参数,但它的成本稍微的编码,所以我preFER这种方式需要$域的元素和$值是相应对,preferably他们将在完全相同的顺序作为原始数组$纪录。

While I can use named parameters but it would cost slightly more coding so I prefer this way which requires the elements of $fields and $values are in corresponding pairs, preferably they would be in exactly same order as that of the original array $record.

任何想法?

推荐答案

是的,他们是。事实上,这并不是在手动提到,所以我指的是内部实现。这里的样品<一个href=\"http://lxr.php.net/xref/PHP_TRUNK/ext/standard/array.c#2424\"><$c$c>array_keys():

Yes, they are. Indeed, that's not mentioned in manual, so I'm referring to internal implementation. Here's sample for 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);
    }

- 嗯,是的,code以上是在C,但它肯定说明什么是函数内部的内在逻辑。我觉得 LXR 是搜索非常友好的 - 所以我省略了诸如宏 - 定义(它们都关闭了这个问题) - 但你可以去更深入和调查全貌

-well, yes, code above is in C, but it definitely shows what's internal logic inside the function. I think lxr is very friendly for search - so I omit such things as macro-definitions (they are off this question) - but you can go deeper and investigate full picture.

这篇关于被责令元素在同一阵列被array_keys()和array_values​​()拆分后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆