Back to questions
JuniorTheoryCommon
What does the program print?
<?php
$stock = ['pear' => 2, 'apple' => 5];
$stock['kiwi'] = 1;
$stock['pear'] = 7;
unset($stock['apple']);
foreach ($stock as $fruit => $qty) {
echo $fruit, '=', $qty, ' ';
}
echo count($stock), "\n";pear=7 kiwi=1 2
- Expecting
$stock['pear'] = 7to movepearto the end: writing to an existing key changes the value in place, and the pair keeps the position it was added at. - Expecting
unsetto leave a gap thatcountstill counts:unset($stock['apple'])removes the key with its value, socountreturns2.
- How would you move
pearto the end of the array? - What does
isset($stock['apple'])return after theunset, and why?