Hello.

I just used array_flip() and it left out one of the elements 4th from the bottom.

The original array has 15 elements and the flipped array has 14.

Has anyone heard of this? Any suggested methods to accomplish the flip?

I'm using PHP 5.2.13.

Thanks.

    Can you show us a concise code snippet and corresponding output that exhibits this issue?

      Thanks for your reply, bradgrafelman.

      Here you are:

        unset($_POST['shuffle_locn_22'], $_POST['unused_liner'], $_POST['button']);
        reset($_POST);
        $Revised_Post = $_POST;
        $Revised_Post_flip = array_flip($Revised_Post);
      
          [project] => Project ABC
          [grtg] => Mr.
          [fname] => First
          [lname] => Last
          [title] => Title
          [company] => Company
          [address] => 123 ABC St.
          [city] => Anycity
          [state] => State
          [zip] => 99999
          [phone] => 999-999-9999
          [fax] => 999-999-9999
          [email] => abcde@example.com
          [web] => http://www.example.com
          [comments] => testing data array flip2
      
      
      )
      
      Array
      (
          [Project ABC] => project
          [Mr.] => grtg
          [First] => fname
          [Last] => lname
          [Title] => title
          [Company] => company
          [123 ABC St.] => address
          [Anycity] => city
          [State] => state
          [99999] => zip
          [999-999-9999] => fax
          [abcde@example.com] => email
          [http://www.example.com] => web
          [testing data array flip2
      
      ] => comments
      

      Thanks.

        [man]array_flip/man loses entities because your original data had duplicate values (e.g. for 'phone' and 'fax'). When you use the values as keys for the array, these duplicates are lost since keys in an array must be unique.

          Wow... I forgot about that bradgrafelman, but...

          How to structure the flip for the case when someone's phone and fax are the same which I see quite often.

          Thanks.

            I don't understand why you're trying to flip the array at all. Can you explain why this is even necessary or desired?

              I'm writing data to a file and writing the column names (1st line of the file) using the key names. I flipped and used implode with a delimiter to line everything up. I can't hardcode the column names because the data is coming from $_POST which could change in the future.

              Is there a better way to create the column headings from the keys of the associative array and with a delimiter?

              Thanks.

                Use [man]implode/man on [man]array_keys/man instead.

                EDIT: Actually, if you're writing a CSV-like file, you should use [man]fputcsv/man instead of [man]implode/man.

                  Got it. I'll give it a go.

                  Thanks bradgrafelman!

                    That worked beautifully, bradgrafelman.

                    Thanks!

                    I will change this thread status to "Resolved!'

                      Write a Reply...