Jump to content

Echoing/retrieving an array problem


dink87522

Recommended Posts

$a = $landSellCheck[0];
echo("<p>a: $a");
$a = $landSellCheck[1];
echo("<p>a: $a");
$a = $landSellCheck[2];
echo("<p>a: $a");

$i = 0;
$landSellCheck = $landSellCheck[$i];
echo("<p>b: (0): $landSellCheck");
$i = 1;
$landSellCheck = $landSellCheck[$i];
echo("<p>b: (1): $landSellCheck");
$i = 2;
$landSellCheck = $landSellCheck[$i];
echo("<p>b: (2): $landSellCheck");

 

As can be seen from the output below when I echo my array by manually inserting the index number, it works fine as it should. If I set the index number as a variable and echo it, it works for index value 0, for index value 1 is gives 'a' somehow (no idea where that comes from) and for index value 2 it is empty. What am I doing wrong?

 

Output is:

 

a: landSellConfirm7

 

a: landSellConfirm6

 

a: landSellConfirm1

 

b: (0): landSellConfirm7

 

b: (1): a

 

b: (2):

Link to comment
Share on other sites

You're using the same variable on both sides of the assignment operator, thereby overwritting the array with the first element of the array on the first iteration through the loop.

 

On the second iteration through the loop, $landSellCheck contains the string 'landSellConfirm7' and $landSellCheck[$i] references the second letter of that, which is the 'a', and assigns that to $landSellCheck.

 

On the third iteration through the loop, $landSellCheck contains the string 'a' and $landSellCheck[$i] references a the third letter of that, which does not exist, and you get nothing (if you were doing this on a system with error_reporting set to E_ALL and display_errors set to ON you would be getting an invalid index error.)

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.