-1

I'm in need of retrieving individual data from a php array. This is how my array looks like:

Array ( 
    [0] => stdClass Object ( 
        [membername] => Moni
        [memberid] => 5
        [membertype] => 1
        [roleid] => 1
    )
) 

I got this by writong this print_r($result);

This is how I tried to retrieve individual data:

$login = TableRegistry::get('login');
$result = $login->loginuser($loginid, $password);

print_r($result);
echo $result->memberid;

But, I get this error: Trying to get property of non-object [APP/Controller\LoginController.php, line 17]

This is line-17: echo $result->memberid;

So, how can I do this? How can I retrieve individual data?

Blaatpraat
  • 2,829
  • 11
  • 23
SSS
  • 703
  • 4
  • 11
  • 23

2 Answers2

3

just replace echo $result->memberid; by echo $result[0]->memberid;

Pradyut Manna
  • 588
  • 1
  • 3
  • 12
3

With that array, you can get it this way:

$login = TableRegistry::get('login');
$result = $login->loginuser($loginid, $password);

if (isset($result[0]) && isset($result[0]->memberid)) {
    echo $result[0]->memberid;
}

Hence the extra check that I provided so that the code can run without errors.

Blaatpraat
  • 2,829
  • 11
  • 23