I hope this is a newbie problem.

I've got a text file with a list of Key/Value pairs extracted from an HTML form on my hosted web server. I send this file as an email attachement to my local computer and am running a script that puts these values into an an associative array. Later in the script I access the values one at a time in the array and send them to my local database (using prepared statements with bound parameters and a MySQL database).

The Key/Value pairs in the email-attached file look like this:

"FirstName"=>"Mary"
"LastName"=>"Lamb"
"Address"=>"Some Street"
...and so forth...

Here is the script I wrote to put these values into an associative array

<?php>
$ff = file('list.txt');  // list.txt is the file attached to the email; ff=form fields
foreach($ff as $key=>$value) {
    $ffe = explode("=>", $value);  // ffe=form fields exploded
    $ffa[$mfe[0]]=$ffe[1];  // ffa=form fields associative array
}
?>

When I use iterative commands, like print_r, foreach($ff as $key=>$value), and array_keys, I can see that the associative array is constructed correctly, but when I use non-iterative commands like

array_key_exists("FirstName", $ffa);

I get a return of

false 

and

echo $ffa["FirstName"];

returns

null

Is this a known aspect of the language or am I missing something?

BTW, I am successfully using parse_str() to create an associative array that does allow non-iterative access to the Key/Value pairs (after reformatting the file attached to the email). Still, I am curious about this discrepancy in the associative array's readability.

    Please ignore the extra

    >

    in the opening

    <?php

    tag of the code snippet.

      If the file has quote marks around the keys, then those quote marks will be in the array keys as well.

      Incidentally, you might like to look at [man]parse_ini_file[/man].

        Huge thanks, Weedpacket.

        As you pointed out, the iterative functions do in fact show double quotes around the Keys and Values in the array.

        So, I took the double quotes off the Key/Value pairs in the target file and ran

        array_key_exists("FirstName", $ffa); 

        and

        echo $ffa["FirstName"];

        but no joy - they still fail to find the item in the array.

        I tried all the functions (iterative and non-iterative) on six different versions of the target file, using all combinations of double quotes, single quotes, and no quotes around the Key/Value pairs. Plus, I did the test run against the six files twice, using the functions first with single quotes and then with double quotes around the Key value.

        The iterative functions work fine on all of the six file formats, but the non-iterative functions fail as before.

        In any case, I looked at the parse_ini_file function you suggested, and with a little reworking of the target file to conform to the php.ini formatting, that function works like a charm, and with a much more readable target file than parse_str requires.

        I'm still curious about the discrepancy I'm experiencing, but I guess that will require a look at the source code.

        Thanks again.

          Write a Reply...