dink87522 Posted December 11, 2010 Share Posted December 11, 2010 $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): Quote Link to comment https://forums.phpfreaks.com/topic/221272-echoingretrieving-an-array-problem/ Share on other sites More sharing options...
PFMaBiSmAd Posted December 11, 2010 Share Posted December 11, 2010 You are overwriting your array with the first element in the array - $landSellCheck = $landSellCheck[$i] Quote Link to comment https://forums.phpfreaks.com/topic/221272-echoingretrieving-an-array-problem/#findComment-1145605 Share on other sites More sharing options...
dink87522 Posted December 11, 2010 Author Share Posted December 11, 2010 You are overwriting your array with the first element in the array - $landSellCheck = $landSellCheck[$i] I don't get it, I am echoing the variable before overwriting it again though aren't I? Quote Link to comment https://forums.phpfreaks.com/topic/221272-echoingretrieving-an-array-problem/#findComment-1145606 Share on other sites More sharing options...
PFMaBiSmAd Posted December 11, 2010 Share Posted December 11, 2010 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.) Quote Link to comment https://forums.phpfreaks.com/topic/221272-echoingretrieving-an-array-problem/#findComment-1145609 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.