In my php app, I do a db query that returns a bunch of documents from mongodb. As i'm looping through each document, i'm trying to extract the timestamp and display it as a Y-M-d h:i:s format. But I'm not even sure at this point how to extract it.
I'm testing with this code:
var_dump($value->playbook_run_date);
That outputs/ returns this:
object(MongoDB\BSON\UTCDateTime)#9 (1) {
["milliseconds"]=>
string(13) "1486277081000"
}
I've tried to change the code to look like this:
var_dump($value->playbook_run_date['milliseconds']);
But that returns the error:
Cannot use object of type MongoDB\BSON\UTCDateTime as array
I've been googling around for that error and have tried a few examples including:
var_dump($value->playbook_run_date->{'milliseconds'});
var_dump($value->playbook_run_date->1->{'milliseconds'});
var_dump($value->playbook_run_date[1]->{'milliseconds'});
But I can't seem to figure it out.
Any help would be appreciated.
I found the answer. http://php.net/manual/en/mongodb-bson-utcdatetime.todatetime.php
Code looks like this:
$temp = $value->playbook_run_date->toDateTime();
echo ("<td>".$temp->format('r') ."</td>");
Object properties with numerical names need to be referenced as a string surrounded by curly braces:
var_dump($value->playbook_run_date->{'1'}['milliseconds']); // If 'milliseconds is an array key
var_dump($value->playbook_run_date->{'1'}->milliseconds); // If milliseconds is an object property
try
$value->playbook_run_date->__toString();
var_dump($value->playbook_run_date);
That outputs/ returns this:
object(MongoDB\BSON\UTCDateTime)#9 (1) {
["milliseconds"]=>
string(13) "1486277081000"
}
You can try this:
//--first deserialize the MonogoDB date object
$playbook_run_date_unix_timestamp = $value->playbook_run_date->jsonSerialize()['$milliseconds'];
//--then you can work with the results any how you want
$playbook_run_date_datetime_format = date("Y-M-d h:i:s", ($playbook_run_date_unix_timestamp/1000) );
Related
I'm working on an AJAX CRUD and I cannot get the form values in the Assoc. array to save individually as object attributes for the MySQL query.
I am following enter link description here but instead of the mysqli I'm using PDO.
Not much of a php person and this is my first OOP use of PDO and JSON.
The vardump() shows the input text is there...
// get posted data
$data = json_decode(file_get_contents("php://input"), true);
// set event property values
$event=>mainTitle = $data->main-title;
$event->subTitle = $data->sub-title;
$event->eventUrl = $data->event-url;
And the dumps:
array(9) {
["main-title"]=>
string(15) "Test Main Title"
["sub-title"]=>
string(14) "Test Sub title"
["event-url"]=>
string(9) "Test URTL"
...
object(Event)#3 (11) {
["conn":"Event":private]=>
object(PDO)#2 (0) {
}
["table_name":"Event":private]=>
string(8) "tblEvent"
["mainTitle"]=>
int(0)
["subTitle"]=>
int(0)
["eventUrl"]=>
int(0)
...
try changing $event=>mainTitle to $event->mainTitle
You have passed a second argument to json_decode() what means you'd like to get an array instead of the object. So just work like with the array. Replace to $event->mainTitle = $data['main-title'];
I found the answer for part of my problem here: Handling-JSON-like-a-boss-in-PHP
json_decode() is able to return both an object or associative array.
//For an object:
$result = json_decode($string);
//For an Assoc. Array:
$result = json_decode($string, true);
What I struggled with is that the var_dump() returns almost exact result for the two. They indeed have to be the same type.
The second part of my problem that was more subvert was having hyphens in the object attribute names. I didn't find a reason why but for sake of clarity in my code I just removed them.
i have an array after convert it from xml data and i have var_dump the data in array like :
object(SimpleXMLElement)#651 (1) {
["authenticationSuccess"]=>
object(SimpleXMLElement)#652 (1) {
["user"]=>
string(9) "yassine"
}
}
i want to get the value the attribut user that equal "yassine" in this cas .
i trying
$xml["authenticationSuccess"]["user"]
but not working , it return null value , is there any solution to get this data from the array .
some help please
It seems your variable is not array but object, so you need to use $xml->authenticationSuccess->user;
As the var_dump says, you have an object instead of an associative array. You can access object fields like this:
$xml->authenticationSuccess->user;
or this:
$xml->{"authenticationSuccess"}->{"user"};
What I'm trying to Do is so simple yet everything I tries had failed. I have the following string: "{"msg":"background1.jpg"}", and I want to convert it to an array to access the msg value. this should simply be done like so(or so I've thought):
$theString = "{"msg":"background1.jpg"}";
var_dump(json_decode($theString, TRUE));
The vr_dump() is dumping NULL, also tried:
var_dump(json_decode(json_encode($theString), TRUE));
This dumps string(45) "{"msg":"background1.jpg"}"
and tried many many more things, but all failed. Any thought please.
EDIT:
I'm getting the json string from database, where i have previously stored like so:
$toBeStored = json_encode(array("msg" => $value));
Try this
$var = '{"msg": "background1.jpg"}';
var_dump($var);
`
I'm returning some data, and testing my return by using var_dump($this);. It returns the following:
object(ReportingService)#333 (2) {
["_arrErrors"]=>
array(0) {
}
["nameWS"]=>
string(14) "reportingstuff"
}
{"arrMessages":[{"_strMessage":"Example.","_strType":"valid","_strModule":null}],"arrContent":{"isSuccess":"1","statistics":"<div id=\"entities\">
What I am trying to do is to access the nameWS property of the object, but cannot seem to do so.
What I've tried:
var_dump($this[0]->nameWS);
Use the following:
$this->nameWS;
You find more information in the manual Setting and Getting Object Properties
You can access it via:
$this->nameWS;
The zero you were using is for the array in "_arrErrors", so you don't need that.
echo $this->nameWS;
will output:
reportingstuff
In my php file im using the following,
$obj = ($_POST['data']);
var_dump(json_decode($obj,true));
And i see this result. Is this the correct format? and how do i access the array.
eg, set a new variable $newID the same as row1 id
array(4) {
["row0"]=>
string(92) "{"id":"157","name":"123","basic":"123123123","submitter":"Keith","status":"review"}"
["row1"]=>
string(169) "{"id":"158","name":"TEST RESOURCE","basic":"Please state the type of work.","submitter":"Keith","status":"review"}"
["row2"]=>
string(107) "{"id":"159","name":"TEST OTHER","basic":"testing for other","submitter":"Keith","status":"review"}"
["row3"]=>
string(160) "{"id":"160","name":"Name","basic":"type of work","submitter":"Keith","status":"review"}"
}
heres whats in POST in firebug
data {"row0":"{\"id\":\"157\",\"name\":\"123\",\"basic\":\"123123123\",\"submitter\":\"Keith\",\"status\":\"review\"}","row1":"{\"id\":\"158\",\"name\":\"TEST RESOURCE\",\"basic\":\"Please state the type of work.\",\"submitter\":\"Keith\",\"status\":\"review\"}","row2":"{\"id\":\"159\",\"name\":\"TEST OTHER\",\"basic\":\"testing for other\",\"submitter\":\"Keith\",\"status\":\"review\"}","row3":"{\"id\":\"160\",\"name\":\"Name\",\"basic\":\"type of work\",\"submitter\":\"Keith\",\"status\":\"review\"}"}
Each "row" of the array is another JSON string. It seems like the data was double-encoded, like:
$array = json_encode(
array(
'row0' => json_encode(array('id' => '157', ...)),
...
)
)
This is incorrectly encoded data, unless you wanted JSON objects inside JSON objects. To work with it, you need to json_decode each individual item again. Better though: fix the encoding step.