...

View Full Version : array_combine modification



jjfletch
09-24-2006, 06:00 AM
When I print_r($_POST) to determine what's being posted from my form, I get this:



Array ( [name] => Array ( [0] => Joe [1] => [2] => [3] => [4] => [5] => ) [animal] => Array ( [0] => Dog [1] => Cat [2] => [3] => [4] => [5] => ) [submit] => Validez )


Then, I array_combine to (duh) combine the arrays, and I get this:


Array ( [Joe] => Dog [] => )


I'd like to get this:


Array ( [Joe] => Dog [] => Cat)

Anyone know how I can accomplish this?

Fumigator
09-24-2006, 06:45 AM
If I understand you right, you want to combine the elements in the array assigned to $_POST['name'] and the elements in the array assigned to $_POST['animal'], so that the $_POST['name'] elements become the keys and $_POST['animal'] elements become the values.

Is that right?

I think array_combine will work for you. You said you tried it but you didn't show us the code you used.


if (is_array($_POST['name']) && is_array($_POST['animal'])) {
$newArray = array_combine($_POST['name'], $_POST['animal']);
}

That should work fine.

p.s. Wrapping print_r() in <pre> tags makes it readable.


print "<pre>\n";
print_r($_POST);
print </pre>\n";



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum