Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,902 questions

51,833 answers

573 users

How to create an array by using one array for keys and another for the values in PHP

4 Answers

0 votes
$arr_key = array('green', 'red', 'yellow');
$arr_val = array('1', '2', '3');
$ac = array_combine($arr_key, $arr_val);

print_r($ac);  



/*
run:

Array ( [green] => 1 [red] => 2 [yellow] => 3 ) 

*/


answered Apr 5, 2015 by avibootz
edited Dec 14, 2023 by avibootz
0 votes
$ac = array_combine(Array('a','b','c'), Array(1,2,3));

print_r($ac);  



/*
run:

Array ( [a] => 1 [b] => 2 [c] => 3 ) 

*/


answered Apr 5, 2015 by avibootz
edited Dec 14, 2023 by avibootz
0 votes
print_r(array_combine(Array('a','b','c'), Array(1,2,3)));  



/*
run:

Array ( [a] => 1 [b] => 2 [c] => 3 ) 

*/


answered Apr 5, 2015 by avibootz
edited Dec 14, 2023 by avibootz
0 votes
$ac = array_combine(Array('a','b','c'), Array(1,2,3,4,5));

print_r($ac);  



/*
run:

Warning: array_combine(): Both parameters should have an equal number of elements...

*/


answered Apr 5, 2015 by avibootz
edited Dec 14, 2023 by avibootz
...