Jump to content

Array and variable help


gsingh85

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
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?
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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'
)
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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