views:

67

answers:

2

The weirdest thing is happening to me...

I have a form I'm sending via an ajax post (using jquery's serialize function) to a php script running this function (stripped down for clarity):

            $arr = $_POST;
            unset($arr['command']);
            unset($arr['index']);
            $vals = $arr;
            $keys = $arr;


            $keys = array_flip($keys);

            return 'vals= ' . implode(',',$vals) . '      keys = ' . implode(',',$keys);

The String I sent that works looks like this...

alt text

that gives me the result...

alt text

now when I Remove the "S" from "About" (in the title field) I get the data string that looks like this: alt text

that gives me THIS result: alt text

The "Title" key has been completely stripped out of the equation!

Any Ideas what could be happening?

+3  A: 

While flipping an array if a value has several occurrences, the latest key will be used as its values, and all others will be lost.

An example(from manual)

<?php
$trans = array("a" => 1, "b" => 1, "c" => 2);
$trans = array_flip($trans);
print_r($trans);
?>

Output:

Array
(
    [1] => b
    [2] => c
)
codaddict
you are amazing. thank you.
Jascha
On unrelated note, PHP also has array_keys() function, if you need all the array keys as values.
Anti Veeranna
+1  A: 

array_flip is not just returning an array of the array keys. Instead, it flips the mapping of keyvalue to valuekey. And when an array with duplicate values is flipped, only the latest key will be used:

If a value has several occurrences, the latest key will be used as its values, and all others will be lost.

Now if you just want the array keys, use array_keys instead.

Gumbo
I gave an upvote for pointing out array_keys... coaddict answered my question first so I had to give him the checkmark, but thank you.
Jascha