4

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
  • I have problems to understand the problem. Do you want to get only the first value of the array and send it to a function or do you want to send every value to a function? Commented Feb 13, 2010 at 22:21
  • As nobody is linking to it: Documentation of the foreach control structure: php.net/manual/en/control-structures.foreach.php Commented Feb 13, 2010 at 22:23

3 Answers 3

2

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]);        
}
Sign up to request clarification or add additional context in comments.

2 Comments

What if it ia associated array?
@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).
2

$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
}

1 Comment

Ok the Comment // Do something is funny, i think there should be a control statement am i wrong?
0

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

echo current($array);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.