1

I looked a question in php related with array. But the question is in array of array. The output of the question is "78". But I don't know how it is work. Please explain it...

<?php
    $arr= array(1,2,3,5,8,13,21,34,55);
    $sum = 0;

    for($i=0; $i<5; $i++)
    {
        $sum += $arr[$arr[$i]];
    }
        echo$sum;
?>
6
  • $arr[$i] is just the expression you use as the key of $arr?! Commented Dec 21, 2016 at 19:46
  • Could you please explain? Commented Dec 21, 2016 at 19:56
  • 1
    $sum += $arr[$arr[$i]] is the same as $index = $arr[$i]; $sum += $arr[$index]; Commented Dec 21, 2016 at 19:58
  • Well you loop through the numbers 0-4. You use those as index for $arr with the variable $i. So you access the first 5 array elements of the array and use those again as key again. Means the first 5 elements 1,2,3,5,8 are used as key again with which you access the numbers: 2,3,5,13,55 and add them together. Commented Dec 21, 2016 at 19:59
  • @Rizier Thank you for your grand visualize in Sam Onela's answers. Commented Dec 21, 2016 at 20:22

2 Answers 2

4

It is adding the element at index $arr[$i], which is not the same as the element at $i.

╔═════════════╦════╦═══════════════════╦══════════════╦══════╗
║ Iteration   ║ $i ║ $index = $arr[$i] ║ $arr[$index] ║ $sum ║
╠═════════════╬════╬═══════════════════╬══════════════╬══════╣
║ before loop ║ -  ║ -                 ║ -            ║ 0    ║
║ 1           ║ 0  ║ 1                 ║ 2            ║ 2    ║
║ 2           ║ 1  ║ 2                 ║ 3            ║ 5    ║
║ 3           ║ 2  ║ 3                 ║ 5            ║ 10   ║
║ 4           ║ 3  ║ 5                 ║ 13           ║ 23   ║
║ 5           ║ 4  ║ 8                 ║ 55           ║ 78   ║
╚═════════════╩════╩═══════════════════╩══════════════╩══════╝

You can see this illustrated in this phpfiddle example.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your kind explantion.
2

I changed some variable names and spaced actions out for clarity, and added a lot of comments. I hope this helps clarify what's happening inside the for loop :).

<?php
  /**
   * Think of this as [0 => 1, 1 => 2, ...8 => 55,] or more abstractly as
   * [index => value, index => value] where Array indices
   * start at 0 and climb by every additional value.
   */
  $arrayVariable = [1,2,3,5,8,13,21,34,55,];
  $sumOfArrayParts = 0;

  /* Use for loop to create a bounded iteration (in this case run 5 times) */
  for ($arrayIndex = 0; $arrayIndex < 5; $arrayIndex++) {
    /**
     * Separating this into a separate step for clarity, 
     * set the index to whatever number is at the index given by $arrayIndex 
     */
    $chosenIndex = $arrayVariable[$arrayIndex];
    /* Index Values: 1, 2, 3, 5, 8 */
    $chosenNumber = $arrayVariable[$chosenIndex];
    /* Number Values: 2, 3, 5, 13, 55 */

    /* Add current value at array index */
    $sumOfArrayParts += $chosenNumber;
    /**
     * Iteration values:
     * 1) 0 + 2         // $sumOfArrayParts = 2
     * 2) 2 + 3         // $sumOfArrayParts = 5         
     * 3) 5 + 5         // $sumOfArrayParts = 10
     * 4) 10 + 13       // $sumOfArrayParts = 23
     * 5) 23 + 55       // $sumOfArrayParts = 78
     */
  }
  echo $sumOfArrayParts;
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.