Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/290950-array-and-variable-help/
Share on other sites

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 by requinix

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 by gsingh85

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'
)

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.

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.