PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
Cirdan
Forum Contributor
Posts: 144 Joined: Sat Nov 01, 2008 3:20 pm
Post
by Cirdan » Mon Apr 12, 2010 1:42 pm
I'm trying to foreach loop on an array, but it says invalid argument for foreach. I don't understand this.
When I print_r the array, it looks like this:
Code: Select all
Array ( [Africa/Abidjan] => Abidjan [Africa/Accra] => Accra [Africa/Addis_Ababa] => Addis Ababa [Africa/Algiers] => Algiers [Africa/Asmara] => Asmara [Africa/Asmera] => Asmera [Africa/Bamako] => Bamako [Africa/Bangui] => Bangui [Africa/Banjul] => Banjul [Africa/Bissau] => Bissau [Africa/Blantyre] => Blantyre [Africa/Brazzaville] => Brazzaville [Africa/Bujumbura] => Bujumbura [Africa/Cairo] => Cairo [Africa/Casablanca] => Casablanca [Africa/Ceuta] => Ceuta [Africa/Conakry] => Conakry [Africa/Dakar] => Dakar [Africa/Dar_es_Salaam] => Dar es Salaam [Africa/Djibouti] => Djibouti [Africa/Douala] => Douala [Africa/El_Aaiun] => El Aaiun [Africa/Freetown] => Freetown [Africa/Gaborone] => Gaborone [Africa/Harare] => Harare [Africa/Johannesburg] => Johannesburg [Africa/Kampala] => Kampala [Africa/Khartoum] => Khartoum [Africa/Kigali] => Kigali [Africa/Kinshasa] => Kinshasa [Africa/Lagos] => Lagos [Africa/Libreville] => Libreville [Africa/Lome] => Lome [Africa/Luanda] => Luanda [Africa/Lubumbashi] => Lubumbashi [Africa/Lusaka] => Lusaka [Africa/Malabo] => Malabo [Africa/Maputo] => Maputo [Africa/Maseru] => Maseru [Africa/Mbabane] => Mbabane [Africa/Mogadishu] => Mogadishu [Africa/Monrovia] => Monrovia [Africa/Nairobi] => Nairobi [Africa/Ndjamena] => Ndjamena [Africa/Niamey] => Niamey [Africa/Nouakchott] => Nouakchott [Africa/Ouagadougou] => Ouagadougou [Africa/Porto-Novo] => Porto-Novo [Africa/Sao_Tome] => Sao Tome [Africa/Timbuktu] => Timbuktu [Africa/Tripoli] => Tripoli [Africa/Tunis] => Tunis [Africa/Windhoek] => Windhoek ) 1
AbraCadaver
DevNet Master
Posts: 2572 Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:
Post
by AbraCadaver » Mon Apr 12, 2010 1:46 pm
Need to see the array assignment code and the foreach.
mysql_function(): WARNING : This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Cirdan
Forum Contributor
Posts: 144 Joined: Sat Nov 01, 2008 3:20 pm
Post
by Cirdan » Mon Apr 12, 2010 1:50 pm
Code: Select all
<? foreach($this->getTimezones() as $country => $zones) :?>
<?=$country?>
<? foreach($zones as $timezone => $city) : ?>
// This is the invalid foreach
<? endforeach; ?>
<? endforeach; ?>
Code: Select all
public function getTimezones() {
$timezones = DateTimeZone::listIdentifiers();
$i = 0;
foreach($timezones as $zones)
{
$zone = explode('/', $zones);
$zone2[$i]['continent'] = isset($zone[0]) ? $zone[0] : '';
$zone2[$i]['city'] = isset($zone[1]) ? $zone[1] : '';
$zone2[$i]['subcity'] = isset($zone[2]) ? $zone[2] : '';
$i++;
}
asort($zone2);
$options = array();
$options['UTC'] = 'UTC';
foreach($zone2 as $zone)
{
extract($zone);
if($continent === 'Africa' || $continent === 'America' || $continent === 'Antarctica' ||
$continent === 'Arctic' || $continent === 'Asia' || $continent === 'Atlantic' ||
$continent === 'Australia' || $continent === 'Europe' || $continent === 'Indian' ||
$continent === 'Pacific')
{
if(isset($city) != '')
{
if($subcity != '')
$options[$continent][$continent.'/'.$city.'/'.$subcity] =
str_replace('_', ' ', $city.'/'.$subcity);
else
$options[$continent][$continent.'/'.$city] = str_replace('_', ' ', $city);
}
}
}
return $options;
}
AbraCadaver
DevNet Master
Posts: 2572 Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:
Post
by AbraCadaver » Mon Apr 12, 2010 1:58 pm
The array you posted is a single dimension, so you only have one dimension to loop through, not two. Is the array from this?
mysql_function(): WARNING : This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Cirdan
Forum Contributor
Posts: 144 Joined: Sat Nov 01, 2008 3:20 pm
Post
by Cirdan » Mon Apr 12, 2010 2:00 pm
The array I posted is from $zones
AbraCadaver
DevNet Master
Posts: 2572 Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:
Post
by AbraCadaver » Mon Apr 12, 2010 2:05 pm
Cirdan wrote: The array I posted is from $zones
Paste me a small sample array from DateTimeZone::listIdentifiers(); and I'll check it.
mysql_function(): WARNING : This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Cirdan
Forum Contributor
Posts: 144 Joined: Sat Nov 01, 2008 3:20 pm
Post
by Cirdan » Mon Apr 12, 2010 2:25 pm
From DateTimeZone::listIdentifiers();
Code: Select all
Array ( [0] => Africa/Abidjan [1] => Africa/Accra [2] => Africa/Addis_Ababa [3] => Africa/Algiers [4] => Africa/Asmara [5] => Africa/Asmera [6] => Africa/Bamako [7] => Africa/Bangui [8] => Africa/Banjul [9] => Africa/Bissau [10] => Africa/Blantyre [11] => Africa/Brazzaville [12] => Africa/Bujumbura [13] => Africa/Cairo [14] => Africa/Casablanca [15] => Africa/Ceuta [16] => Africa/Conakry [17] => Africa/Dakar [18] => Africa/Dar_es_Salaam [19] => Africa/Djibouti [20] => Africa/Douala [21] => Africa/El_Aaiun [22] => Africa/Freetown [23] => Africa/Gaborone [24] => Africa/Harare [25] => Africa/Johannesburg...
AbraCadaver
DevNet Master
Posts: 2572 Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:
Post
by AbraCadaver » Mon Apr 12, 2010 3:21 pm
Here is the array returned by $this->getTimezones():
[text] [UTC] => UTC
[Africa] => Array
(
[Africa/Abidjan] => Abidjan
[Africa/Accra] => Accra
[Africa/Addis_Ababa] => Addis Ababa
[Africa/Algiers] => Algiers
etc...
)
etc...[/text]
You can't foreach over the UTC item because it isn't an array. So you either need to remove it or find a different way to build this array. Or maybe:
Code: Select all
if(is_array($zones)) {
//foreach($zones as $timezone => $city)
}
mysql_function(): WARNING : This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Cirdan
Forum Contributor
Posts: 144 Joined: Sat Nov 01, 2008 3:20 pm
Post
by Cirdan » Mon Apr 12, 2010 5:31 pm
Awesome, thanks for you help!