0

I'm writing a PHP plugin and need to extract a string from an object which is inside an array.

var_dump($data)

outputs:

array(1) { 
    [0]=> object(stdClass)#380 (53) { 
        ["id"]=> string(1) "2" 
        ["firstname"]=> string(6) "John" 
        ["lastname"]=> string(6) "Doe" 
        ["email"]=> string(31) "johndoe@email.com" 
    } 
} 

I want to return:

johndoe@mail.com

My research has turned up functions such as unserialize, array_map, array_slice, and array_column, but I haven't found the right function or combination of functions that work together (nor do I understand them enough) to just return a simple string from an object inside an array. Can anyone provide me with some guidance?

Edit: This isn't the same question as the one referenced as "the exact same question." The other one asks simply how to access an array (answer: $array[0]), and my question asked how to access an object INSIDE an array (answer: $array[0]->text).

Kimber Warden
  • 151
  • 1
  • 14

1 Answers1

1

For example:

$data[0]->firstname

Maybe:

$arr = get_object_vars($data[0]);
$arr['firstname'];

Example:

$obj = (object) array('x' => 'foo');
$arr = [$obj];

print($arr[0]->x);

You will get: foo

Peter
  • 748
  • 6
  • 20