Zen Cart Logo
Forums / Code Collaboration / Replacing while with foreach?

Replacing while with foreach?

Views: 921

Results 1 to 2 of 2
12 Feb 2018, 18:37
#1
tmccaff avatar

tmccaff

Zen Follower

Join Date:
Jan 2018
Posts:
157
Plugin Contributions:
0

Replacing while with foreach?

I have this code and I am confused how to change this to foreach

reset($currencies->currencies); 
	$currencies_array = array(); 
	while (list($key, $value, $title) = each($currencies->currencies))

Is this correct?

$currencies_array = array(); 
        foreach ($currencies->currencies as $key => $value) {
12 Feb 2018, 19:03
#2
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,065
Plugin Contributions:
56

Re: Replacing while with foreach?

tmccaff:

I have this code and I am confused how to change this to foreach

reset($currencies->currencies);
$currencies_array = array();
while (list($key, $value, $title) = each($currencies->currencies))

> 
> Is this correct?
> 
> ```php
$currencies_array = array(); 
        foreach ($currencies->currencies as $key => $value) {

Close, try this instead:

$currencies_array = array();
foreach ($currencies->currencies as $currency) {
    list($key, $value, $title) = $currency;
}