gsingh85 Posted September 9, 2014 Share Posted September 9, 2014 Hi Everyone Please checkout the below code: foreach ($joiners as $joiner) { $town = explode(',', $joiner['address']); if (isset($town[2])) { $town = $town[2]; $get_available_towns[] = $town; $get_available_towns = array(); } foreach ($joiners as $joiner) { $town = explode(',', $joiner['address']); if (isset($town[2])) { $town = $town[2]; $get_available_towns[] = $town; $get_available_towns = array(); } I don't really understand what is happening. I know the code and everything is correct and it works but I really don't understand how the loop, arrray and variable is working. I wanted to print_r the $get_available_towns outside the loop but it would not work without the square brackets but also I did not even know the square brackets were required. The point was to loop through the data and print all the values. I understand how loops, array's and variables work but I've not seen them used like this. This makes me think I am missing some basic knowledge but I have looked around and I can't see any examples of how loops and arrays are used like this. I have also checked the php manual but still no joy. Can you guys tell me where I could find the information that would have examples on how something like the above code would work? Quote Link to comment Share on other sites More sharing options...
requinix Posted September 9, 2014 Share Posted September 9, 2014 (edited) It's not actually correct. $get_available_towns[] = $town; $get_available_towns = array();It adds a value to the $get_available_towns array, but then resets it immediately after. The second line should be positioned higher: before the loop even begins. That will set the variable to be an empty array, avoiding warnings about trying to add values to an array that doesn't exist, and then not reset it during the loop. $get_available_towns = array(); foreach ($joiners as $joiner) { $town = explode(',', $joiner['address']); if (isset($town[2])) { $town = $town[2]; $get_available_towns[] = $town; } }Does that maybe clear things up a bit? Edited September 9, 2014 by requinix Quote Link to comment Share on other sites More sharing options...
gsingh85 Posted September 9, 2014 Author Share Posted September 9, 2014 (edited) Thanks very much for your reply. It does and it doesn't although your explanation is perfect. What does this actually mean? $get_available_towns[] = $town; Do you know a good article that could explain this is detail or a good text book with this section because I'm fairly new to this and although I have used arrays and loops I've never had to add value to arrays or create empty arrays. Edited September 9, 2014 by gsingh85 Quote Link to comment Share on other sites More sharing options...
CroNiX Posted September 9, 2014 Share Posted September 9, 2014 It adds a new element to the end of $get_available_towns array. $towns = array('New York', 'Portland', 'Boston'); //Add Chicago to the $towns array $towns[] = 'Chicago'; $towns now equals array( 0 => 'New York', 1 => 'Portland', 2 => 'Boston', 3 => 'Chicago' ) Quote Link to comment Share on other sites More sharing options...
CroNiX Posted September 9, 2014 Share Posted September 9, 2014 The problem you were having here: $get_available_towns[] = $town; $get_available_towns = array(); Is that you add $town to the $get_available_towns array, which is fine, but then you reset the $get_available_towns array to be empty immediately after that so it will never contain anything. Quote Link to comment Share on other sites More sharing options...
gsingh85 Posted September 9, 2014 Author Share Posted September 9, 2014 Oh I see. Do think I should read up on adding elements to arrays? Quote Link to comment Share on other sites More sharing options...
jcbones Posted September 9, 2014 Share Posted September 9, 2014 Since arrays are a very powerful tool in most programming and scripting languages, yes, I would suggest learning about them. Quote Link to comment 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.