CodeIgniter Forums

Full Version: How to get value from array? message: "Cannot use object of type stdClass as array"
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
How to get value [id] from array [0].


Code:
$a = ($details->production_companies);

print_r($a);
// result from print_r($a)
Array (
    [0] => stdClass Object (
        [id] => 420
        [logo_path] => /hUzeosd33nzE5MCNsZxCGEKTXaQ.png
        [name] => Marvel Studios
        [origin_country] => US )
    [1] => stdClass Object (
        [id] => 2
        [logo_path] => /4MbjW4f9bu6LvlDmyIvfyuT3boj.png
        [name] => Walt Disney Pictures
        [origin_country] => US )
)


echo $a[0]['id'];
// if i use this code results message: (Cannot use object of type stdClass as array)
[SOLVED]
That's easy.

PHP Code:
$details->production_companies[0]->id;

// Or

$a = ($details->production_companies);
$a[0]->id
(10-02-2018, 02:50 AM)kaitenz Wrote: [ -> ]That's easy.

PHP Code:
$details->production_companies[0]->id;

// Or

$a = ($details->production_companies);
$a[0]->id

before i use this code "$a[0]->id;" but message error.
but after I tried again it turned out to be. Surprisingly.

there might be an error when defining $a. hehe

Thanks