Jump to content

Archived

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

jjfletch

array_combine modification

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?

Share this post


Link to post
Share on other sites
Hint - use pre tags around print_r(), it makes it much more legible.
[code]
echo '<pre>', print_r($_POST, true), '</pre>';
[/code]

Share this post


Link to post
Share on other sites
If I have

[code]<?php
$a = array (0 => 'Joe', 1 => '');
$b = array (0 => 'Dog', 1 => 'Cat');
?>[/code]

Then
[code]<?php
$c = array_combine($a, $b);
?>[/code]

--> [pre]
Array
(
    [Joe] => Dog
    [] => Cat
)[/pre]

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites
He is trying to combine, except where I get
[pre]
Array
(
    [Joe] => Dog
    [] => Cat
)[/pre]

he gets
[pre]
Array
(
    [Joe] => Dog
    [] =>
)[/pre]

Share this post


Link to post
Share on other sites
same here Barand, when I run it I get

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

Share this post


Link to post
Share on other sites

×

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.