Say that I have an associative array of different types of fruits in baskets and the amount in each basket ('Banana'=>50 and etc) and I need to know how many of each type of fruit there is, but I don't know the possible types of fruit before I start the loop that generates the array. This may result in duplicate entries such as:
$baskets = array(
"Apple"=>"35",
"Banana"=>"37",
"Apple"=>"43"
);
Question A: Is it even possible to make the second "Apple" entry or will it result in an error?
The answer was: Not possible, it would overwrite the first one with 43
Question B1: Assuming that the answer to A is "It's possible", how do I merge the two so that $baskets becomes ("Apple"=>"78","Banana"=>"37")?
Question B2: Assuming that the answer to A is "Not possible", how do I go about figuring out if there's already apples in the array and add the amount to the entry rather than attempting to add it as a new point in the array?
An answer to B2 will obviously make B1 obsolete even if A is "possible".
If neither B1 nor B2 are possible, then is there some other way that I can keep track of the amount of each type of fruit as I loop through the baskets?
Thanks for all of the helpful answers.
if (isset($baskets[$currentFruit])) { // add to existing entry } else { // add new fruit entry}.