Guest User

Untitled

a guest
Jul 7th, 2018
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. // Your array
  2. $friends = array(
  3. array ('fname' => 'Dana', 'lname' => 'Severance', 'email' => 'dana.severance@gmail.com'),
  4. array ('fname' => 'Chris', 'lname' => '', 'email' => 'laskey@gmail.com'),
  5. array ('fname' => 'Cory', 'lname' => 'Honkala', 'email' => ''),
  6. );
  7.  
  8. // Your foreach
  9.  
  10. foreach ($friends as $friends) {
  11.  
  12. echo "Here should be the name: " . $friends[$i]['fname'] . "<br /> Number: " . $i . "<br /><br />";
  13. $i++;
  14. }
  15.  
  16. // Same loop, different variable names for clarity and to illustrate a point
  17. foreach ($friends as $key => $value) { // could also be foreach ($friends as $value) {
  18.  
  19. // for each loop through the array, two variable are initialized, one, $key, holds the key, and $value holds the value of that spot in the array
  20. // So, the [$i] part of your code points to a non-existant array
  21. echo "Here should be the name: " . $value['fname'] . "<br />Number: ". $i ."<br /><br />";
  22.  
  23. // $value => Array ('fname' => 'Dana', 'lname' => 'Severance', 'email' => 'dana.severance@gmail.com'),
  24. // On iteration two, $value => Array ('fname' => 'Chris', 'lname' => '', 'email' => 'laskey@gmail.com'),
  25. // On iteration three, $value => Array ('fname' => 'Cory', 'lname' => 'Honkala', 'email' => '')
  26.  
  27. }
Add Comment
Please, Sign In to add comment