Jump to content

Multi dimensional array


stubarny

Recommended Posts

Hi All,

 

I have the code below which returns "<br>line 82, dalmonfishing<br>".

 

Please could you explain why it doesn't return "<br>line 82, salmonfishing<br>"? (i.e. why is the s getting replaced with a d?)

 

$_SESSION[process_sign_up][sign_up_email]="salmonfishing";
$_SESSION[process_sign_up][sign_up_email][test_result_detail]="duplicate";
echo "<br>line 82, " . $_SESSION[process_sign_up][sign_up_email] . "<br>";

 

Thanks,

 

Stu

Link to comment
https://forums.phpfreaks.com/topic/264660-multi-dimensional-array/
Share on other sites

You're attempting to treat the string value in the ['sign_up_email'] index as an array and push the value "duplicate" onto it. You can't add array elements to a string value. Also, you should have error_reporting set to -1 and display_error set to On while developing. All of your array indices are throwing undefined constant notices because they aren't quoted.

Thanks Pikachu2000 :-)

 

Regarding the quotations in the indices please could you tell me what are the practical implications of not using quotations are? I noticed that my arrays worked fine without the quotations so I stopped using them to improve the readabililty of my code.  :o

 

Thanks,

 

Stu

Without the quotes, php first looks for a defined constant matching a string value in the array index. It eats processor cycles in doing so and when the constant isn't found, eats more processor cycles generating the notice. Suppressing the errors doesn't change that.

 

Run this code and see how the output it generates is different from what you may expect:

 

define('bob', 'jones');
$names = array('bob' => 'smith', 'jones' => 'richard');

echo $names[bob];
echo "<br>";
echo $names['bob'];

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.