Jump to content


Photo

array_combine modification


  • Please log in to reply
5 replies to this topic

#1 jjfletch

jjfletch
  • Members
  • Pip
  • Newbie
  • 7 posts

Posted 24 September 2006 - 04:59 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?

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 24 September 2006 - 09:24 AM

Hint - use pre tags around print_r(), it makes it much more legible.
echo '<pre>', print_r($_POST, true), '</pre>';

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 24 September 2006 - 09:44 AM

If I have

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

Then
<?php
$c = array_combine($a, $b); 
?>

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

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#4 paul2463

paul2463
  • Members
  • PipPipPip
  • Advanced Member
  • 955 posts
  • LocationUK

Posted 24 September 2006 - 09:44 AM

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:-


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


will result, when you print_r it


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


you cannot affect the past but you can ruin a perfectly good present by worrying about the future

*********************************************************************
The <A HREF="http://www.php.net/d...php">MANUAL</A> is actually a useful resource

#5 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 24 September 2006 - 09:52 AM

He is trying to combine, except where I get
[pre]
Array
(
    [Joe] => Dog
    [] => Cat
)[/pre]

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


If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#6 paul2463

paul2463
  • Members
  • PipPipPip
  • Advanced Member
  • 955 posts
  • LocationUK

Posted 24 September 2006 - 10:30 AM

same here Barand, when I run it I get

Array
(
    [Joe] => Dog
    [] => Cat
)
you cannot affect the past but you can ruin a perfectly good present by worrying about the future

*********************************************************************
The <A HREF="http://www.php.net/d...php">MANUAL</A> is actually a useful resource




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users