Q:

array within indexed numbers

I tried to test a new thing and just saw an unusual output I am unable to get what exactly is happening within the loop can anyone explain me what happens if we take an array and assign that array to a variable we run a for loop and we provide $array and index of $array with the inititlizer for forloop something like this $array[$array[$i]] so I mean I am confused to totally explain but can you review the code and let me know what exactly is happening
$array = array(1, 2, 3, 5, 8, 13, 21, 34, 55);

$sum = 0;

for($i = 0; $i < 5; $i++) {
    $sum += $array[$array[$i]];
}
echo $sum ;

The output 78 as it added the value but if i remove $sum+= and write down like this 
  echo $array[$array[$i]] . "<br />"; 

so now what here is I will be getting output like this
2
3
5
13
55
I am unable to get what exactly hapend within this loop

A:

You are using the value of the array to access the key.
$array = array(1, 2, 3, 5, 8, 13, 21, 34, 55);

$sum = 0;

for($i = 0; $i < 5; $i++) {
    $sum += $array[$array[$i]] . "<br />";
}
echo $sum ;

Loop 1:
$i = 0;
$sum += $array[$array[0]];
$sum += $array[1];
$sum += 2;

Loop 2:
$i = 1;
$sum += $array[$array[1]];
$sum += $array[2];
$sum += 3;

Loop 3:
$i = 2;
$sum += $array[$array[2]];
$sum += $array[3];
$sum += 5;

Loop 4:
$i = 3;
$sum += $array[$array[3]];
$sum += $array[5];
$sum += 13;

Loop 5:
$i = 4;
$sum += $array[$array[4]];
$sum += $array[8];
$sum += 55;

So...
$sum = 2 + 3 + 5 + 13 + 55 = 80

