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"};
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'm working with a multidimensional object/array and want to retrieve the value of the key labeled confirm_enabled and store it within a variable. Here is the object:
object(stdClass)#361 (2) { ["id"]=> string(1) "1" ["meta"]=> string(475) "{"feed_name":"Default Feed","auto_respond":"0","push_Salesforce":"0","lookup_enabled":"1","confirm_enabled":"1","voterdata_mapped_fields*":"9","voterdata_mapped_fields*":"10","voterdata_mapped_fields_*":"11","voterdata_mapped_fields_*":"","voterdata_mapped_fields_*":"","voterdata_mapped_fields_*":"13","voterdata_mapped_fields_*":"6"}" }
Does anyone know how I can do this?
Try this:
$retrieve = (theVariable)->meta->confirm_enabled;
print_r($retrieve);
Or if you want to make all that an array use this:
$a = json_encode((the_fetch_data)) // insert where you get the data
$b = json_decode($a) // then decode to make this all an array
print_r($b['meta']['confirm_enabled']); //show data
I've figured it out! I'd forgotten to json_decode() before attempting to parse. I queried my database to retrieve the object, which is stored in $meta. From there, I just did the following:
$meta = json_decode( $meta[0]->meta );
$confirm_enabled = intval( $meta->confirm_enabled );
I hope this helps someone!
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'].
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];
Whenever i build a URL i.e.
cart.php?action=add&p[]=29&qty[]=3&p[]=5&qty[]=13
and try to grab the p variable and the qty variable, the = 'Array'
var_dump =
array(3) { ["action"]=> string(3) "add" ["p"]=> string(5) "Array" ["qty"]=> string(5) "Array" }
I create half the URL with PHP, and the other half is concatenated with Javascript.
P and QTY are Arrays because you created them using the variable[] syntax. And when you try to turn an array into a string, PHP just use's 'Array'. Echoing something turns it into a string, and then prints it to the string.
The [] tells PHP to make a new key in the array numerically, and assign the value to it.
If you want to get acess of the values of p, go like this
foreach($_GET['p'] as $value)
{
// $value is one of the values of the array, and it goes through all of them
}
The foreach iterates through all of the values of the array, where $value is the value of the current element you are working on.
If you want to access the first value assigned to p, use
echo $_GET['p'][0];