I need to access the array coordinates in this:
object stdClass Object (
[type] => Point
[coordinates] => Array (
[0] => 53.64203491
[1] => -8.52337036
)
)
But no matter what I try I can't get it. Any ideas? I am using PHP.
If the name of the variable is $data, then you can access the the coordinates array as follows:-
$coordinatesArr = $data->coordinates;
I think this should work. If it does not, please share the peice of code you are using.
Try something like this: $stdObject->coordinates[0] and $stdObject->coordinates[1].
Related
Hello I have this json:
[{"org_src":"img\/base\/logo.png","src":"\/cache\/300-logo.png"},
{"org_src":"\/img\/l2.JPG","src":"\/cache\/6l-2.JPG"},
{"org_src":"\/img\/studio\/desk.JPG","src":"\/cache\/desk.JPG"},
...
How looks the array before its is decoded?
If I use Json_encode to this, I will get this:
Array
(
[0] => stdClass Object
(
[og_src] => img/base/logo.png
[src] => /cache/300-logo.png
)
[1] => stdClass Object
(
[og_src] => /img/l2.JPG
[src] => /cache/6l-2.JPG
)...
What is stdClass Object?
Thanks for any help in advance.
with json_decode($test,true); I will get this:
Array
(
[0] => Array
(
[og_src] => img/base/logo.png
[src] => /cache/logo.png
)
[1] => Array
(
[og_src] => /img/studio/l2.JPG
[src] => /cache/6l-2.JPG
...
This do not help me to see how the origin array looks like.
Here is the answer. That what I looked for. Thanks for any suggestion.
$stack[0][org_src]= "Hallo";
$stack[0][src] = "scrkjh";
$stack[1][org_src] = "Halfgfglo";
$stack[1][src] = "scrkjh";
json_encode($stack);
You probably meant json_decode
If you look in the docs, you will notice that there is assoc parameter if you want to get associative array instead of an object.
So you should do
$data = json_decode($data, true);
if you don't want objects
stdClass is the base class of all objects in PHP. If functions like json_decode() create objects that are not of a special class type or other data types will be casted to object, stdClass is used as the data type.
You can compare it to the Object data type in Java.
You asked for an example how to access stdClass's properties, or in general, object properties in PHP. Use the -> operator, like this:
$result = json_decode($json);
// access 'src' property of first result:
$src = $result[0]->src;
stdClass is php's generic empty class, kind of like Object in Java or object in Python
It is useful for anonymous objects, dynamic properties, etc. and Stdclass Object is its object
See Dynamic Properties in PHP and StdClass for example
Also.. As Marko D said, You can use json_decode($data, true); true to get an associative array instead of object.
I can't seem to get specific data from an array inside an object.
$this->fields->adres gets the address correctly, but i can't get a level deeper.
I've tried:
$this->fields->province
$this->fields->province->0
$this->fields->province[0]
And: (edit)
$this->fields["province"][0]
$this->fields['province'][0]
$this->data->fields['province'][0]
But it does not return anything while it should return "Flevoland".
First part of the object print_r($this, TRUE) below:
RSMembershipModelSubscribe Object
(
[_id] => 2
[_extras] => Array
(
)
[_data] => stdClass Object
(
[username] => testzz
[name] => testzz
[email] => xxxx#example.com
[fields] => Array
(
[province] => Array
(
[0] => Flevoland
)
[plaats] => tesdt
[adres] => test
You can also use type casting.
$fields = (array) $this->data->fields;
echo $fields['province'][0];
As you can see by your output, object members are likely to be private (if you follow conventions, anyway you must prepend an underscore while calling them), so you're calling them the wrong way;
This code works:
$this->_data->fields['province'][0];
You can see it in action here;
I created a similar object, and using
$membership = new RSMembershipModelSubscribe();
echo $membership->_data->fields['province'][0];
outputs "Flevoland" as expected.
As fields is already an array, try this:
$this->fields['province'][0]
This assuming the [_data] object is $this.
Fields and province are both arrays, you should be trying $this->fields["province"][0]
$this->_data->fields['province'][0]
This is driving me crazy, I am trying to get to a specific part of this object and it is driving me crazy, here is the object contents:
XMLHandler Object
(
[doc:XMLHandler:private] => SimpleXMLElement Object
(
[#attributes] => Array
(
[state] => Live
)
[newsListItem] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[href] => http://api.contentplus.co.uk/6cb5ea15-d6b1-4c40-9db7-cb2a3315080b/news/800773226/
)
[id] => 800773226
[publishDate] => 2011-10-24T10:04:49
[lastModifiedDate] => 2011-10-24T11:20:40
[headline] => Relationships matter on social media
)
)
)
[format] => html
)
I want to get the value of [id] I am trying to access it like this:
echo $niList->doc->newsListItem[0]->id;
but this is giving me nothing, I know I am close (well I hope I am) but I just cant quite get it right, could anyone help please.
Thanks all.
Your object dump says
doc:XMLHandler:private
which means doc is a private property of XMLHandler. As such, you can only access it from within that object via $this. But you are trying to access it from outside the object when you do
echo $niList->newsListItem[0]->id;
This wont work. Add a method to that XMLHandler object that does what you want to do with that newslistitem id. Also see the chapter on Visibility in the PHP Manual:
http://docs.php.net/manual/en/language.oop5.visibility.php
I have a multidim array that I can view using print_r($users):
Array
(
[0] => stdClass Object
(
[username] => crustacea
[created_on] => 2010-08-07 19:54:32
[active] => 1
)
)
I can also use print_r($users[0]). Since there's only one member in $users, the output looks somewhat the same.
I can't see how to retrieve the value crustacea. echo $users[0]['username']; doesn't do it. Examples I can find are echo $users['name_of_array']['username']; -- but I don't have a name of array to work with. How do I do it?
$user[0] is not an array but an object as indicated by [0] => stdClass Object.
echo $users[0]->username;
$user[0] is object and not an array. You can access your value by doing
$users[0]->username;
Or you could cast/convert your object into an array like
((array)$users[0])['username'];
I've got a response form the solr query in php. below is the response form apache solr in drupal6, i need to access the id field inside the Apache_Solr_Document, can some bosy help me with this.
i was able to print this using print_r($result);
Array ( [type] => Bookmarks [node] => Apache_Solr_Document Object ( [_documentBoost:protected] => [_fields:protected] => Array ( [id] => b17692e4ad53/node/274 )))
if i do print_r($result[node]); i am getting
Apache_Solr_Document Object ( [_documentBoost:protected] => [_fields:protected] => Array ( [id] => b17692e4ad53/node/274 ))
from here i can't figure out how to access the id.
Have you tried reading through IBM's article on Solr? The end of the article deals specifically with PHP.
Edit: After finding a source class to read through, it looks like you can do:
$result['node']->getField("id");
or using the magic __get:
$result['node']->id;
You can try using the {} brackets if the object name is not a valid object property name (like SimpleXML stuff).
$object->{'strang&$*#nmame'}->value;