Parsing Json From Web Service With PHP [duplicate] - php

This question already has answers here:
How can I access an array/object?
(6 answers)
Closed 6 years ago.
I'm new with json and web service. After using insert command, I do print_r($result) and got this data:
Array ( [error_code] => 0 [error_desc] => [result] => Array ( [error_code] => 999 [error_desc] => Format input 'date' is wrong ) )
I want to print the error_desc only which is show Format input 'date' is wrong.
I try to parsing it with PHP but I only got empty result. Here is my code:
if (is_array($result)) {
if ($result['error_code'] =='999') {
echo $result['error_desc'];
}
}
Please help. Thanks.

I already got the answer. I just know after print the error_code. Thanks.

Related

json_decode - associative arrays [duplicate]

This question already has answers here:
How to extract and access data from JSON with PHP?
(1 answer)
PHP printed boolean value is empty, why?
(4 answers)
Closed 8 months ago.
I'm trying to store the value of ok in a variable starting from a json file but the output isn't what I'm expecting it to be.Here's my code:
$messaggio = '{"ok":false,"error_code":400,"description":"Bad Request"}';
$messaggio = json_decode($messaggio, true);
print_r($messaggio);
Output: Array ( [ok] => [error_code] => 400 [description] => Bad Request)
Shoudn't it be like this? Array ( [ok] => false [error_code] => 400 [description] => Bad Request)And if it shouldn't, how can I store the value of ok in a variable?
This is the content of a script copied here and pasted in a php script:
$messaggio = '{"ok":false,"error_code":400,"description":"Bad Request"}';
$messaggio = json_decode($messaggio, true);
print_r($messaggio);
var_export($messaggio['ok']);
As you can see, ... I've added var_export function to export the value.
Array
(
[ok] =>
[error_code] => 400
[description] => Bad Request
)
false
If you see nothing, it doesn't means that there is nothing.
I've also tried to show you a little example with interactive shell.
php -a
Interactive shell
php > $variabile = false;
php > echo $variabile;
php > var_export($variabile);
false
If you want to go deep into the content of a variable. Use var_export or var_dump functions.

Extract value from multi-dimension array in php [duplicate]

This question already has answers here:
How to get single value from this multi-dimensional PHP array [duplicate]
(6 answers)
Closed 3 years ago.
I'm trying to slice a multi-array in php. I'm using google's geo-location api and I've managed to get it to return lat,lng and accuracy. I've used
$message = json_decode($result, true);
To convert the Json into an array.
The problem I have is that I have the results in this form:
Array ( [location] => Array ( [lat] => 10.1453652 [lng] => 0.2338764 ) [accuracy] => 33 )
and I can't figure out how to extract the lat and lon. Can anyone help?
Try the following code:
echo $message['location']['lat'];
It's simple, if not what you are expected, its straight forward
echo $message['location']['lat'].' '.$message['location']['lng'];
Working demo.

PHP Pull data out of an array [duplicate]

This question already has answers here:
How to get single value from this multi-dimensional PHP array [duplicate]
(6 answers)
Closed 5 years ago.
I know this should be a relatively simple thing but I have not been able to do it yet. I have look hi and low and every example I try it fails I am sure it is fairly simple
Here is my array. I need to get the value of name last and filenames
Any help would be most appreciated.
Thanks!!
Array
(
[formData] => Array
(
[name] => TEST
[last] => TEST1
[filenames] => Array
(
[0] => /ocdata/uploads/export-1511887767.csv
)
)
)
Really simple method to see your content:
foreach($array as $k => $v)
{
echo $k . $v . PHP_EOL; // see content of your array
}
Or use directly the values:
$array['formData']['name'];
$array['formData']['last'];
$array['formData']['filenames'];

array to get object's value failed in php [duplicate]

This question already has answers here:
Accessing #attribute from SimpleXML
(10 answers)
Closed 8 years ago.
why is this wrong?
[enclosure] => SimpleXMLElement Object
(
[#attributes] => Array
(
[url] => http://www.thestar.com.my/~/media/Images/TSOL/Photos-Gallery/features/2014/07/02/dominiclau020714.ashx?crop=1&w=460&h=345&
[length] =>
[type] => image/jpeg
)
)
I want to get the url to get the image file
I wrote print_r($eachItem->enclosure['#attributes']->url) it doesn't work. Why?
That is not the correct way of getting the attribute value. Use ->attributes() method:
echo (string) $eachItem->enclosure->attributes()['url'];
// as of PHP 5.4 (dereferencing)
Or
// PHP 5.3 below
$eachItem_attribute = $eachItem->enclosure->attributes();
echo (string) $eachItem_attribute['url'];
Right & Quick Format
$eachItem->enclosure->attributes()->{'url'};

php and session: how get an associative array from a session variable? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Able to see a variable in print_r()'s output, but not sure how to access it in code
I need to work on an existing website with a connected user:
<?php session_start();
print_r($_SESSION); ?>
This code outputs:
Array([%http_user%] => Array
(
[profile_id] => 72
[username] => john
[session_id] => 4ek79umrrpael7vvb4ls3diaq4
)
[registration_invite_code] =>
)
That's what I need. It try to get "john" into my $username variable:
any idea how ?
I've tried with:
$_SESSION[0]
$_SESSION["%http_user%"]
$_SESSION->username
No way.
Any way how to retrieve this username ?
Thanks
$variable = $_SESSION['%http_user%']['username'];

Categories