简体   繁体   中英

PHP multidimensional array - second dimension overwriting first value

I have this code which works as I expect:

$somearr['some']='value';

$arr[]="first";
$arr['first'][]=$somearr['some'];

echo "<br> Var dump of arr: " . var_dump($arr);

Yields:

array (size=2)
  0 => string 'first' (length=5)
  'first' => 
    array (size=1)
      0 => string 'value' (length=5)

Perfect..

Now attempting to apply the same principle (I assume) in this:

function get_field_names($database, $table){



$show_statement = "DESCRIBE `" . $database . "`.`" . $table . "`";

$column_result = mysql_query($show_statement);






        if (mysql_num_rows($column_result) > 0) {



            $f = 0;
            while ($row = mysql_fetch_assoc($column_result, MYSQL_ASSOC)) {



                $field_names[$f] = $row['Field'];

                if ($row['Key'] != ""){



                $field_names[$f][0] = $row['Key'];

                }

                $f++;


            }       

            }



    return $field_names;

}

But instead of creating a second dimension to my array $field_names[$f][0] with the value of $row['Key'], instead the first character in the string of $field_names[$f] is overwritten with the first character in $row['Key'] such that $field_names[$f] contains the string: "field_name" and after $field_names[$f][0]= $row['Key'] where $row['Key'] == "PRI", I get : "Pield_name".

I am sure I am misunderstanding the array $row, but I am beginning to go in circles. Please flame me for my stupidity... after you point out my issue. TIA.

That's easily explained by revisiting your original example:

$arr[]="first";
$arr['first'][]=$somearr['some'];

This doesn't do what you think it does. The first line doesn't create a new array key first . It just assigns a value to the indexed entry [0] .
The second line however assigns a value to the alphanumeric key [first] . That's two distinct sub-entries in the $arr array.

In your second example, you are overwriting values:

$field_names[$f] = 'string...';

$field_names[$f][0] = 'something else...';

You can only have one array entry at the [$f] key. You cannot have a shared spot with a string and an array.

What actually happens here is that the second line is assigning a value to the first array-like index of the 'string...' . It's equivalent to the alternative string-index syntax:

$field_names[$f]{0} = 'something else...';
                 ^
                 |
                 Not an array index. Because a string was here first.

Anyway, you cannot share an entry as string and key. That's the whole story. - I think it's cursorily explained somewhere in the manual under array s.

This is because you can treat any string in PHP like an array

$test = "Hello, World";
echo $test[0];

Will result in "H" being echo'ed. What you are telling you code in

$field_names[$f][0] = $row['Key'];

is replace the first character in the string $field_names[$f] with $row['Key']. However since it is only 1 character that we are replacing PHP just uses the first character in $row['Key'] thus you get "Pield_name", the "P" from PRI replaced in for the "F" which was at the first position in the "string array"

I'm still confused however to what you want to do. If you could map out what you want the array to look like after you insert(?) the $row['Key']. I can help you with the code

EDIT: The reason you top code is working is you are simply not specifying a key for your first item you add to the array:

$arr[]="first"; //Inserts string "first" at pos 0 of array same as $arr[0]="first";
$arr['first'][]=$somearr['some']; //Inserts array into $arr using key 'first'

echo "<br> Var dump of arr: " . var_dump($arr);

The same could be achieved with this code

$arr =array(
    0       => 'first',
    'first' => $somearr['some'] 
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM