Alessandro Pietrantonio Alessandro Pietrantonio - 2 years ago 226
PHP Question

Codeigniter: Trying to get property of non object on apparently correct object

I have the following json string:

"{"name":"select","id":"select","class":"form-control select","type":"select","data-input-type":"select","options":"\"Primo:primo\\r\\nSecondo:secondo\""}"

If I json_decode it the result is the following:

object(stdClass)#30 (6) {
string(6) "select"
string(6) "select"
string(19) "form-control select"
string(6) "select"
string(6) "select"
string(32) ""Primo:primo\r\nSecondo:secondo""

It seems to be correct to me.
I'm trying to get the string called "options" and echo it. But when I do
an error appear which say:

A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object

What is wrong with it?

Answer Source

$post_field->meta_value is json string. $post_field->meta_value->options is not. json_decode() function is able to convert json string only, but you were trying to reach it's substring. You have to make object or array first to be able to approach certain property or key.

$meta_value = json_decode($post_field->meta_value);
$options = $metavalue->options;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download