php - Retrieving data from multiple arrays using a while loop -


i'm looking way optimize following code using while loop.. i've got 4 arrays , pull 1st value each array in efficient way. original code works fine:

$arr1 = array ("55", "66", "77"); $arr2 = array ("54", "64", "771"); $arr3 = array ("53", "62", "772"); $arr4 = array ("52", "60", "773"); $x = 1; $result = "null";  echo $arr1[0] . " | " ;  echo $arr2[0]. " | " ;  echo $arr3[0]. " | " ;  echo $arr4[0]. " | " ;  

blow attempt optimize doesn't seems working:

$arr1 = array ("55", "66", "77"); $arr2 = array ("54", "64", "771"); $arr3 = array ("53", "62", "772"); $arr4 = array ("52", "60", "773"); $x = 1; $result = "null";  while($x < 5) { $result = "$arr".$x."[0]"; echo $result; echo " | "; $x = $x +1; } 

the output i'm getting 1[1] | 2[1] | 3[1] | 4[1]
instead of 55 | 54 | 53 | 52 |

thank all!

while($x < 5) {     $result = ${"arr".$x}[0];     echo $result;     echo " | ";     $x++; } 

or neater solution:

$arr1 = array ("55", "66", "77"); $arr2 = array ("54", "64", "771"); $arr3 = array ("53", "62", "772"); $arr4 = array ("52", "60", "773");  for($x=1; $x<5; $x++) {     $result = ${"arr".$x}[0];     echo $result." | "; } 

Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -