Klanestro
Klanestro

Reputation: 3215

Array Loop - get the first value into a foreach loop

I have an array, and I want to get the first value into a foreach loop. then send that value to a function.

Upvotes: 4

Views: 1495

Answers (3)

streetparade
streetparade

Reputation: 32878

The current(); function would return the first value;

echo current($array);

Upvotes: 0

karim79
karim79

Reputation: 342635

I think you mean to use the current 'exposed' offset as your functions' arguments:

foreach($frequency as $i) {        
   showphp_AlexVortaro (getphp_AlexVortaro($i));
   showphp_Smartfm(getphp_Smartfm($i));        
}

or:

for($i=0; $i<count($frequencies); $i++) {        
   showphp_AlexVortaro(getphp_AlexVortaro($frequencies[$i]));
   showphp_Smartfm($frequencies[$i]);        
}

Upvotes: 2

TheGrandWazoo
TheGrandWazoo

Reputation: 2897

$i is the value of your array in the foreach loop. Instead of sending $frequency[$i] you must use '$i'.

If you want to fetch the keys use the following construction:

foreach ($array as $key => $value) 
{
 // Do something
}

Upvotes: 2

Related Questions