PHP foreach loop array error as "Undefined variable" but the value exists

  arrays, foreach, key, loops, php

I have a variable $users with inside an array. Dumping it I have:

array(7) {
  [0]=>
  array(6) {
    ["'ev_id'"]=>
    string(1) "1"
    ["'user_email'"]=>
    string(17) "[email protected]"
    ["'status_role'"]=>
    string(1) "0"
    ["'registration'"]=>
    string(1) "0"
    ["'confirmation'"]=>
    string(1) "0"
    ["'participation'"]=>
    string(1) "1"
  }
  [1]=>
  array(6) {
    ["'ev_id'"]=>
    string(1) "1"
    ["'user_email'"]=>
    string(24) "[email protected]"
    ["'status_role'"]=>
    string(1) "0"
    ["'registration'"]=>
    string(1) "0"
    ["'confirmation'"]=>
    string(1) "0"
    ["'participation'"]=>
    string(1) "1"
  }
  [2]=>
  array(6) {
    ["'ev_id'"]=>
    string(1) "1"
    ["'user_email'"]=>
    string(20) "[email protected]"
    ["'status_role'"]=>
    string(1) "0"
    ["'registration'"]=>
    string(1) "0"
    ["'confirmation'"]=>
    string(1) "0"
    ["'participation'"]=>
    string(1) "1"
  }
}

This is my code:

echo "<pre>";var_dump($users);
foreach ($users as $key => $value) {
echo $value['ev_id'];
}

I am becoming crazy as I want simply loop and echo the "ev_id" value in the array but I receive an error as:

Notice:  Undefined index: ev_id

The value "ev_id" exists, you can see in the dump. Why I receive an Undefined index?

Source: Ask PHP

LEAVE A COMMENT