What is the best way of accessing this objects tag_id?
Array ( [0] => stdClass Object ( [tag_id] => 100001 ) )
Like this:
echo $var[0]->tag_id;
Related
This question already has an answer here:
Remove index based json object name in php [duplicate]
(1 answer)
Closed 1 year ago.
So here we have an array function array_values but it is working if performing on the same array. Over here i have an object and inside that objst i have set of arrays and with few condition i have to unset few of the keys and reindex the array.
stdClass Object
(
[details] => stdClass Object
(
[first] => 159
[events] => Array
(
[0] => stdClass Object
(
[id]=>1,
[name]=>abc
)
[1] => stdClass Object
(
[id]=>2,
[name]=>abc
)
[2] => stdClass Object
(
[id]=>3,
[name]=>abc
)
)
)
)
Now if i unset the 1 key and do json_encode and while printing it by doing json_decode, then it becomes
stdClass Object
(
[details] => stdClass Object
(
[first] => 159
[events] => stdClass Object
(
[0] => stdClass Object
(
[id]=>1,
[name]=>abc
)
[2] => stdClass Object
(
[id]=>3,
[name]=>abc
)
)
)
)
The events node becomes object, I don't want to change it from array to object. My result has to be exactly the same with reindexing the key.
Use array_values() to reindex an array.
$object->events = array_values($object->events);
how get value feild[id] in php code
stdClass Object ( [List_inserted] => Array ( [0] => stdClass Object ( [ID] => 145001 [value] => 40 ) ) [Sucssess] => 1 [ErrorMassage] => OK )
You didn't give us a name of stdClass so I'm assuming it's $stdClass.
$stdClass->List_inserted[0]->ID
Let's break it down;
stdClass Object ( [List_inserted] => Array ( [0] => stdClass Object ( [ID] => 145001 [value] => 40 ) ) [Sucssess] => 1 [ErrorMassage] => OK )
We access objects with a -> and we access arrays with []
The first part tells us it's an object, so it's;
$stdClass->List_inserted
List_inserted is an array thanks to the => Array. We can access this with [0].
$stdClass->List_inserted[0]
Well, List_inserted[0] is an object, thanks too [0] => stdClass Object; and you wanted to access the ID? So we need another ->
$stdClass->List_inserted[0]->ID
I'm trying to access a value from an extremely complicated associative array:
stdClass Object
(
[return] => Array
(
[0] => stdClass Object
(
[entries] => Array
(
[0] => stdClass Object
(
[key] => LAUNCH_DATE
[value] => 2016/07/20
)
[1] => stdClass Object
(
[key] => COUNTRIES
[value] => AU
)
I've tried this to get the value 'AU' but with no luck unfortunately:
$test = $array_store->return->entries->1->value;
echo $test;
This is very difficult and I'm struggling. I managed to extract from a more simple array but this one is too deep. Please help - trying to learn.
Try that:
$test = $array_store->return[0]->entries[1]->value;
echo $test;
I did a SOAP/WSDL request with a PHP script.
I could access an answer which look like this :
stdClass Object
(
[inventory] =>
(
[0] => stdClass Object
(
[location_inventory] => stdClass Object
(
[location] => stdClass Object
(
[organization] => abcdef
[contact] => stdClass Object
(
[contact_details] => stdClass Object
(
[id] => Thomas
)
)
)
[product] => tomatoes
)
)
)
)
The point is that I can't extract the variables "organization", "id", or "product".
What is the difference of syntax when accessing a stdClass Object or an Array ?
I am guessing you are using json_decode to have the object you showed.
If that is the case, try using json_decode( $data, true); which will return a proper array as described in the docs: here.
That way you will be able to access your data the normal array way: $data['inventory']...
Hope that helps
I have got this result using json decode from an api call. but I dont know how to extract "VALUE" from this result..
$obj=json_decode($json_string);
print_r($obj);
stdClass Object ( [status] => OK [data] => stdClass Object ( [trends] => stdClass Object ( [rank] => Array ( [0] => stdClass Object ( [date] => 201011 [value] => 7196 ) ) ) [trends_low_sample] => [query_cost] => 1 [trends_frequency] => monthly ) )
I need only "7196" from this result. how do I do this??
Ah! Based on your updated code you're tring to get the value from PHP not Javascript? Personally I use json_decode($json_string,true); to get an associative array (json_decode), if you do that it should be accessible as:
echo $obj["data"]["trends"]["rank"][0]["value"];
As an object it's accessible as:
echo $obj->data->trends->rank[0]->value;