Jump to content

array_combine modification


jjfletch

Recommended Posts

When I print_r($_POST) to determine what's being posted from my form, I get this:

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

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

[code=php:0]Array ( [Joe] => Dog [] => )[/code]


I'd like to get this:

[code=php:0]Array ( [Joe] => Dog [] => Cat)
[/code]
Anyone know how I can accomplish this?
Link to comment
https://forums.phpfreaks.com/topic/21832-array_combine-modification/
Share on other sites

the array_combine is doing what you asked of it, it is taking the keys from your name array and combinging it with the keys of the animals array, as you only have one element in your name array it will only combine it with one element from your animal array, the first one, which happens to be dog, so after all that it is doing exactly what it is meant to do....

if you wish all the elements from one array to be joined with all the elements of another array then you need:-

[code]

$result = array_merge($array1, $array2);

[/code]

will result, when you print_r it

[code]

Array ( [0] => Joe [1] => Dog [2] => Cat )

[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.