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
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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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'];

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.