简体   繁体   中英

Adding values from one array as keys in new array

I have two arrays the first one has this structure

Parameter Keys array

 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)

The second is

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

I want a new array with keys the same as the values from the first array[0][x], and then values the same as the values from the second array so I get something like for my new array

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

I tried using array combine but it needs the same amount of values and keys.

I just like to say the first array is created from a preg_match_all function

array_combine()是你的答案。

The way you have it, your keys and values wouldn't match up:

<?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
//)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM