Array Loop - Get the first value in a foreach loop

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

  • This does not work

    foreach ($frequency as $i) { showphp_AlexVortaro (getphp_AlexVortaro ($frequency[$i])); showphp_Smartfm(getphp_Smartfm($frequency[$i])); } 
+4
source share
3 answers

I think you want to use the current "exposed" offset as arguments to your functions:

 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]); } 
+2
source

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

If you want to get the keys, use the following construction:

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

Function current(); will return the first value;

 echo current($array); 
0
source

Source: https://habr.com/ru/post/1301219/


All Articles