Accessing a specific object by name/number? - php

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

Related

Saving form values to an Object as attributes (Associative Array, PHP, PDO, lunk head)

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.

trying to get an element from an array in php

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"};

How to extract timestamp from MongoDB object

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) );

read json output in php

how to read below json data in php?
i have "$json = json_decode($data,true); and
i tryied "$json->{'screenShareCode'};" but is is giving me an error? :(
array(5) {
["screenShareCode"]=>
string(9) "887874819"
["appletHtml"]=>
string(668) ""
["presenterParams"]=>
string(396) "aUsEdyygd6Yi5SqaJss0="
["viewerUrl"]=>
string(65) "http://api.screenleap.com/v2/viewer/814222219?accountid=myid"
["origin"]=>
string(3) "API"
}
The output you are showing is not json. It seems to be a print_r'ed array.
See http://json.org/example
Your output is regular array not JSON, so you access it as regular PHP array:
$x = $array['screenShareCode']
You are looking for json_encode (http://de2.php.net/json_encode)
What you posted is an array, not an object. Because you passed json_decode a second parameter of true, it responded with an associative array instead of an object.
To access a property of an associative array, you can do something like $json['screenShareCode'].

Access Object Value

I am trying to pull out a value from inside a larger object. The main object from an xml file via SimpleXML.
When I var_dump($data->extensions->runTime); this section of the object I get:
object(SimpleXMLElement)#21 (1) {
[0]=>
string(8) "2852.462"
}
How can I access that 2852.462??
I have tried everything I can think of, via array [0], even with a foreach statement. I can't figure out how to access only the value.
Cast it to string:
$value = (string)$data->extensions->runTime[0];
Or better to float:
$value = (float)$data->extensions->runTime[0];

Categories