Reputation: 444
I'm looping through some results list that contains information about fruit and who has picked.
Each iteration contains a single fruit and a single person who picked it.
foreach($Jobs as $key => $val) {
//set values
$fruit = $val->FruitName;
$picker = $val->Picker->ForeName;
//build my array here
$myArray[$val->FruitName] = $val->Picker->ForeName;
}
I'm trying to build up an associative array (using fruit names) that holds an array of the picker names, like so;
$myArray = array (
"apple" => array ("Jon","Jo","Dave"),
"pear" => array ("Ben"),
"plumb" => array ("Jane"),
"melon" => array ("Jon","Jo","Dave","Sarah"),
);
The way I'm currently attempting this simply overwrites the existing array of picker names.
Upvotes: 1
Views: 41
Reputation: 96
foreach($Jobs as $key => $val) {
//set values
$fruit = $val->FruitName;
$picker = $val->Picker->ForeName;
//build my array here
$myArray[$val->FruitName][] = $val->Picker->ForeName;
}
Using your example you need to add the [] to the build array line. This is so the names are added to the array rather than overwriting the value.
Upvotes: 2