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.
Related
This question already has answers here:
What does "1" mean at the end of a php print_r statement?
(3 answers)
Closed 5 years ago.
Given the following code:
<?php
$array = [1,2,3,[4,34]];
echo print_r($array);
?>
It results in the following output in the browser:
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => Array ( [0] => 4 [1] => 34 ) ) 1
What does the 1 outside the array, at the end of the output attempting to display? Tried searching the PHP docs, but apparently no such number appeared at the end of their example.
The 1 you see is the truthy value of the print_r() function.
Because print_r() is a function in itself, it evaluates to truthy.
In your example, print_r() runs and outputs your array, followed by echo, which echoes out the truthy value of print_r().
To eliminate this erroneous 1, simply remove the echo from your code (which isn't needed, as print_r() outputs to the DOM by itself):
<?php
$array = [1,2,3,[4,34]];
print_r($array);
?>
Hope this helps :)
This question already has answers here:
What does "1" mean at the end of a php print_r statement?
(3 answers)
Closed 5 years ago.
I have the following PHP code:
<?php
$array = ["test", "1", "2", "3"];
$id = 0;
echo "ID: 1 - <pre>", print_r($array), "</pre><br/>";
When I execute the code, the output is the following:
ID: 1 - Array
(
[0] => test
[1] => 1
[2] => 2
[3] => 3
)
1<br/>
If you look closely, you will notice a number after the array output of print_r. Why is this number showing up and is there a way to stop it from showing up?
Calling print_r() without a second parameter outputs the value and then returns true - which is the 1 your getting in the output. You probably want to use print_r($array, true) to get it to return the value as a string and then be able to wrap it in the HTML tags you want.
print_r() without it's second argument will return boolean true, which turns to "1" when converted to a string.
Add true as the second parameter to make print_r return it's output.
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.
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'};
I am currently using a third party API to find out the status of a game server. The API is via PHP and I am able to pull an Array with relevant information towards the servers status.
Here is my API's code:
#!/usr/bin/php -q
<?php
require('MulticraftAPI.php');
$api = new MulticraftAPI('http://ip.address.goes.here/multicraft/api.php', 'username', 'apikey');
$a = $api->getServerStatus($argv[1]);
print_r($a);
exit();
?>
This script is ran via PHP CLI: php status.php ${server_id} (19 in this case, but that is irrelevant)
The output is the following:
[admin#ns5001896 maps]# php status.php 19
Array
(
[success] => 1
[errors] => Array
(
)
[data] => Array
(
[status] => online
[onlinePlayers] => 0
[maxPlayers] => 32
)
)
My question:
How can I isolate the value of [status] in [data] and print it as the output. Just a single word "online" or "offline"
I have researched and any normal means from slice'ing the array to dumping vars doesn't work. I am kind of at my wits end here.
Thanks for your time!
Did you try this?:
echo $a['data']['status'];