I have finally managed to retrive data from mongoDB within PHP. How ever I am not able to retrieve single elements from this array looking. I can only vardump() the cursor. How is it possible to print single elements from this array that seems to be made up of objects?
object(stdClass)#11 (7) { ["_id"]=> object(MongoDB\BSON\ObjectID)#9 (1) { ["oid"]=> string(24) "5a4a2cf55ff0f310cbf1c3a4" } ["Category"]=> string(9) "Allgemein" ["DateAdded"]=> object(MongoDB\BSON\UTCDateTime)#10 (1) { ["milliseconds"]=> string(13) "1514810613331" } ["Name"]=> string(4) "Welt" ["Website"]=> string(11) "www.welt.de" ["Active"]=> bool(true) ["Country"]=> string(2) "DE" }
I couldnt find anything on goolgle or PHP/mongodb documentation. Why cant I just do $array["_id"]? And how can I retrieve _id for example?
The resource is an object of stdClass. So you need to use:
echo $array->_id;
In case, if you want to use arrays, use get_object_vars() function. That way:
$array = get_object_vars($array);
echo $array["_id"];
And then you can use objects as arrays.
Related
I have a simple answer I just can't solution to:
var_dump(obj) =
object(stdClass)#15 (3) {
["properties"]=>
object(stdClass)#14 (2) {
["user_name"]=>
string(4) "somename"
["email_address"]=>
string(12) "test#test.com"
["arrays"]=>
object(stdClass)#17 (1) {
["sites[]"]=>
object(stdClass)#18 (4) {
["0"]=>
int(1)
["1"]=>
int(1)
["2"]=>
int(0)
["3"]=>
int(0)
}
}
}
How to call the 'sites[]' object in my 'obj'?
I tried the following:
obj->sites[]
obj->{'sites[]'}
Both options aren't working...
It might be better to clean up the code that generates that object, but you should be able to access the sites[] object via:
$sites = $obj->arrays->{'sites[]'};
However $sites will still be an object, so you would need to access its elements in a similarly awkward way:
echo $sites->{'0'};
It would be better to cast it to an array at that point:
$sites = (array) $obj->arrays->{'sites[]'};
Then you can access as an array:
echo $sites[0];
EDIT, seams you cannot access array elements indexed by a numerical string.
A better option (as discovered by a SO question i just posted about this) would be to use get_object_vars:
$sites = get_object_vars($obj->arrays->{'sites[]'});
Then you can access as an array:
echo $sites[0];
I have written an xml-parser to read an XML-file. The XML-file is not mine so I can't change the structure. Things work great till I got to this special point. I want to read a value but I don't have a key to access this value.
I marked the values (in red) in the screenshot below which I want to access.
When I dump the parent element (the PRAT->VALUE) I get this in return:
object(SimpleXMLElement)#31 (3) { ["#attributes"]=> array(5) { ["nr"]=> string(1) "1" ["unit"]=> string(3) "bar" ["unit_id"]=> string(4) "3103" ["vo"]=> string(0) "" ["vo_id"]=> string(0) "" } [0]=> string(2) "20" [1]=> string(1) "2" }
As seen, at the end of the dump the values that I want to access are presented. I tried to access it like an array but that doesn't work. The values are not part of the attributes.
use (String) keyword in front of it.
eg.
echo (String) PRAT->VALUE;
I am trying to decode a series of JSON messages
The JSON messgaes are received on an adhoc basis
If the JSON messages were all exactly the same format I would be fine. However they are not.
Here is an example of two different types.
object(stdClass)#11 (1) { ["SF_MSG"]=> object(stdClass)#12 (5) { ["time"]=> string(13) "1407962618000" ["area_id"]=> string(2) "NM" ["address"]=> string(2) "2B" ["msg_type"]=> string(2) "SF" ["data"]=> string(2) "FE" } }
object(stdClass)#13 (1) { ["CA_MSG"]=> object(stdClass)#14 (5) { ["to"]=> string(4) "0360" ["time"]=> string(13) "1407962618000" ["area_id"]=> string(2) "WH" ["msg_type"]=> string(2) "CC" ["descr"]=> string(4) "2S30" } }
One has a header of SF_MSG and the other CC_MSG
My question is "How do I in PHP distinguish between the different headers so I can read them in different ways
So for example echo '$Decodedjson->SF_MSG->time; will echo the time on the first one and
echo $Decodedjson->CC_MSG->to; will echo the to variable. However I need to know whether the header is SF_MSG or a CC_MSG before performing that task.
How do I read that Header title????? ie is it CC_MSG or SF_MSG ......$Decodedjson->?????
Thanks
EDIT
if ($con->hasFrame()){
$msg=$con->readFrame();
foreach (json_decode($msg->body) as $event) {
if(isset($event['SFG_MSG'])) {
$aid=($event->SF_MSG->area_id);
}
elseif(isset($event['CA_MSG'])) {
$aid=($event->CA_MSG->area_id);
}
echo $aid;
json_decode() requires true as the second parameter in order for the resultant object to be an associative array.
foreach (json_decode($msg->body, true) as $event)
once you do this, $event will be an associative array and no longer an object, so you will need to change the rest of your code to access $event as an array instead of an object.
// this won't work
$aid=($event->SF_MSG->area_id);
// change to this
$aid = $event["SF_MSG"]["area_id"];
I have been trying to figure this out for a while now. Please any advice would be appreciated. I just need to access the array for ["Item"]. How do I gain access to this?
array(1) {
[0]=>
object(SimpleXMLElement)#16 (2) {
["#attributes"]=>
array(2) {
["Name"]=>
string(10) "AuthorList"
["Type"]=>
string(4) "List"
}
["Item"]=>
array(3) {
[0]=>
string(9) "Smith, Joe"
[1]=>
string(10) "Peter, Ann"
[2]=>
string(18) "Magoo, Mr"
}
}
}
Assuming this structure is in a variable called var1, you should be able to access Item using:
$var1[0]->Item // returns the array
I try to explain it.
Like you see, your first array index is an object.
If you see something like this in your var_dump than you can access it through deferencing the object.
It's the same like you would create an object and would like to access a public variable:
$var1 = new Object();
// when your Object variables are public so you could access them by deference the Object
echo $var1->myVariable; // will echo the public variable "myVariable"
So the answer from adam is the right one :)
I cannot figure out to get these values from this array. I need to know the code and name so my application knows which one to go for but I can't get the values out of there. Can someone please help me out? It's the values in the #attributes.
I'm using PHP by the way.
Thanks
array(2) {
[0]=>
object(SimpleXMLElement)#22 (2) {
["#attributes"]=>
array(2) {
["code"]=>
string(3) "HCD"
["name"]=>
string(31) "HIGH COST DELIVERY REGION SRCHG"
}
[0]=>
string(5) "71.25"
}
[1]=>
object(SimpleXMLElement)#24 (2) {
["#attributes"]=>
array(2) {
["code"]=>
string(3) "HCD"
["name"]=>
string(31) "HIGH COST DELIVERY REGION SRCHG"
}
[0]=>
string(5) "71.25"
}
}
Using SimpleXML, you're able to access the attributes on an XML by using the elements as if it was an array ($xml->elementName['attributeName'], or using the ->attributes() method as previously given).
For example, given the following code:
$xml = new SimpleXMLElement('<root><item foo="bar"></item></root>');
If I wanted to access the foo attribute on the item element, I would access it like this:
echo $xml->item['foo'];
However, there's a catch: the value that is returned is actually an instance of SimpleXMLElement. To be able to store or use it, you will need to convert it to a primitive type:
echo (string)$xml->item['foo']; // now a string
echo (int)$xml->item['foo']; // now an integer
echo (bool)$xml->item['foo']; // now a boolean
I figured it out on my own.
$xmlResponse->AccessorialCharges->OtherAccessorialCharges[$i]['code'];