Log in

View Full Version : Combine arrays?



jacksont123
02-26-2007, 11:12 PM
Say I have these two arrays as an example,


$href = array('1.html', '2.gif', '3.php');
$name = array('My html file', 'My gif file', 'My php file');

How can I combine them to make that?:


$combined = array("1.html" => "My html file", "2.gif" => "My gif file", "3.php" => "My php file")

Twey
02-26-2007, 11:35 PM
$combined = array();
for($i = 0; $i < count($href); ++$i)
$combined[$href[$i]] = $name[$i];

saideep2007
05-25-2010, 07:17 AM
a very Nice solution to combine array...thnks buddy

alexjewell
05-25-2010, 02:54 PM
actually, PHP has a built in function for this called array_combine():



$href = array('1.html', '2.gif', '3.php');
$name = array('My html file', 'My gif file', 'My php file');
$combined = array_combine($href,$name);


Link: http://www.php.net/manual/en/function.array-combine.php

james438
05-25-2010, 07:42 PM
yes, but not in the way that the original poster wanted.

Anyway, these responses are to a thread that is already over 3 years old and solved for that matter.

djr33
05-25-2010, 07:50 PM
It is an old thread, but that is a better answer. As far as I can tell it is exactly what the original poster wanted: array_combine($keys,$values);

james438
05-25-2010, 09:56 PM
I see now. I was confusing array_combine with array_merge.

djr33
05-25-2010, 10:30 PM
Yes, I did too at first. I'm going to close this discussion now so it'll fall back into the archives-- and now the question is fully answered.