views:

44

answers:

1

hi guys, i am trying to create array like in the example i wrote above:

$arr=array('roi sabah'=>500,yossi levi=>300,dana=>700);

but i want to create it dynamic with foreach.

how can i do it ?

thanks.

A: 

You can access an array with foreach (and optionally create another one while going through the array's values). But if you only have data like a name and a number from your example, not stored in an array, you can't use foreach.

Another way to create the array you mentioned:

$arr = array();
$arr['roi sabah'] = 500;
$arr['yossi levi'] = 300;
// etc

And to access these values:

foreach ($arr as $key => $value) {
  // $key is e.g. "roi sabah", and its value "500"
}
Alec