tags:

views:

84

answers:

3

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

  • This does not work

    foreach ($frequency as $i) {        
       showphp_AlexVortaro (getphp_AlexVortaro ($frequency[$i]));
       showphp_Smartfm(getphp_Smartfm($frequency[$i]));        
    }
    
+2  A: 

$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
}
TheGrandWazoo
Ok the Comment // Do something is funny, i think there should be a control statement am i wrong?
streetparade
+2  A: 

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]);        
}
karim79
What if it ia associated array?
streetparade
@streetparade, I'm assuming that `$frequencies` refers to a numerically indexed array of floats (or strings), e.g. 96.5 or '96.5' (I arrived at that assumption by assuming that `getphp_Smartfm` returns something radio related based on a given frequency).
karim79
A: 

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

echo current($array);
streetparade