duannan3959 2014-10-08 16:16
浏览 161
已采纳

将array_keys和array_values组合到原始数组中? [重复]

I have a method that inserts data into a table using an associative array that looks like this:

array(
    "col1" => "value1",
    "col2" => "value2",
    "col3" => "value3",
);

If I use array_keys() and array_values() on the array passed in can I guarantee that the first item in each set, match the original array passed in?

// Note: validation excluded
$keys   = array_keys($data);   // $data is the array passed in
$values = array_values($data);
$q      = array_pad(array(), count($data), "?");

$this->query("insert into `$this->table` (`" . implode("`,`", $keys) . "`) values (" . implode(",", $q) . ")", $values);

So, with that, can I guarantee that $keys[1] and $values[1] are the same key and value from the original array, or is it possible that they could be col3 and value1?

Another way to say it, if i use array_combine($keys, $values) will I get the original array key => value pair back (item order excluded)?

I am worried that if this doesn't do what I am thinking that value2 may go into col3 instead of col2 or something like that....

</div>
  • 写回答

2条回答 默认 最新

  • dttvb115151 2014-10-08 16:26
    关注

    Yes, the functions array_keys($data) and array_values($data) will return the data in the original order.

    A search of the PHP reference site for the array_combine() method will show that the method combines the arrays in their original order as well.

    For example,

    array(
        'red' => 5,
        'green' => 10,
        'blue' => 15
    );
    

    would be split into the keys array:

    array(
        'red',
        'green',
        'blue'
    );
    

    and the values array:

    array(
        5,
        10,
        15
    );
    

    Combining these arrays with array_combine($keys, $values) would give you the array:

    array(
        'red' => 5,
        'green' => 10,
        'blue' => 15
    );
    

    Examples from PHP site

    Array Keys PHP reference

    <?php
        $array = array(0 => 100, "color" => "red");
        print_r(array_keys($array));
    
        $array = array("color" => array("blue", "red", "green"),
               "size"  => array("small", "medium", "large"));
        print_r(array_keys($array));
    ?>
    

    The above example will output:

    Array
    (
        [0] => 0
        [1] => color
    )
    Array
    (
        [0] => color
        [1] => size
    )
    

    Array Values PHP reference

    <?php
        $array = array("size" => "XL", "color" => "gold");
        print_r(array_values($array));
    ?>
    

    The above example will output:

    Array
    (
        [0] => XL
        [1] => gold
    )
    

    Array Combine PHP reference

    <?php
        $a = array('green', 'red', 'yellow');
        $b = array('avocado', 'apple', 'banana');
        $c = array_combine($a, $b);
    
        print_r($c);
    ?>
    

    The above example will output:

    Array
    (
        [green]  => avocado
        [red]    => apple
        [yellow] => banana
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度