【问题标题】:Adding values from one array as keys in new array将一个数组中的值添加为新数组中的键
【发布时间】:2012-05-01 08:27:35
【问题描述】:

我有两个数组,第一个有这个结构

参数键数组

 array
   0 => 
    array
    0 => string ':user_pass' (length=10)
    1 => string ':user_id' (length=8)
    2 => string ':user_name' (length=10)
  1 => 
   array
    0 => string 'user_pass' (length=9)
    1 => string 'user_id' (length=7)
    2 => string 'user_name' (length=9)

第二个是

 array
  0 => string 'test' (length=4)
  1 => string 'test' (length=4)
  2 => string '1' (length=1)

我想要一个新数组,其键与第一个数组 [0][x] 中的值相同,然后值与第二个数组中的值相同,所以我得到了类似于我的新数组的东西

 array
  :user_id => string '1' (length=1)
  :user_name => string 'test' (length=4)
  :user_pass => string 'test' (length=4)

我尝试使用数组组合,但它需要相同数量的值和键。

我只想说第一个数组是从 preg_match_all 函数创建的

【问题讨论】:

    标签: php arrays


    【解决方案1】:

    array_combine() 是你的答案。

    【讨论】:

    • 对不起,我的意思是说我使用了数组组合,但它需要相同数量的键和值。
    【解决方案2】:

    按照你的方式,你的键和值不匹配:

    <?php
    $keys = array(
        array(':user_pass', ':user_id', ':user_name'),
        array( 'user_pass',  'user_id',  'user_name')
    );
    
    $values = array('test', 'test', 1);
    
    print_r(array_combine($keys[0], $values));
    
    //Outputs:
    //Array
    //(
    //    [:user_pass] => test
    //    [:user_id] => test
    //    [:user_name] => 1
    //)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-18
      • 2021-09-13
      • 2020-12-08
      • 2019-09-03
      相关资源
      最近更新 更多