Home > database >  turning php array foreach loop into for loop $key - $value
turning php array foreach loop into for loop $key - $value

Time:02-06

<?php
$money = ["Ahmed" => 100, "Sayed" => 150, "Osama" => 100, "Maher" => 250];

// Output
foreach ($array as $key => $value)
{
echo <<<"hello"
The Name Is $key And I Need $value Pound From Him
<br>
hello;
}
?>

In php how can i make this foreach loop with for loop to get $key and $value separated

CodePudding user response:

is this what you are asking for

<?php
$money = ["Ahmed" => 100, "Sayed" => 150, "Osama" => 100, "Maher" => 250];

// Output
$keys = array_keys($money);
$values = array_values($money);

for ($i = 0; $i < count($keys); $i  ) {
    echo "The Name Is " . $keys[$i] . " And I Need " . $values[$i] . " Pound From Him<br>";
}
?>
  • Related