i'm using facebook fql to get some data , these data are in array
when i use json to decode the result it gives me users id in that style
$result = json_decode($result, true);
the result from array is 1.0000148607246E+14
instead of 10000148607246
i know its the same number but when i use that result to get new data from facebook "request back" it gives me error with id
the question now is how to convert 1.0000148607246E+14 to 10000148607246 in php
Add the precision setting on to the top of the PHP tag.
<?php
echo $var=1.0000148607246E+14; //"prints" 1.0000148607246E+14
ini_set('precision', 18); // <------------------ Add this on to the top of your code
echo $var=1.0000148607246E+14; //"prints" 1.0000148607246
number_format(1.0000148607246E+14,0,'','');
You can read more here
Output
100001486072460
Related
I have an api that returns a json where I want to get some data. When I visit the url from the browser I get the following json:
{
"myarray":{
"_id":"12345qwer",
"value":"https://www.awildurl.com/q/12345678/test-1-1234-123.html",
"provider":"foo",
"price":null
}
}
I get the data in php from the server and print with:
$json_content = file_get_contents($jsonurl);
$json_decoded=json_decode($json_content, TRUE);
echo "provider: ".$json_decoded['myarray']['provider']."<br>"; //foo
The problem appears when I try to get and print the url (value), I get a null variable:
echo "value: ".$json_decoded['myarray']['value']."<br>"; //foo
Also when I try to print_r the array right after getting the url in php I get null in the point where the actual url should be:
print_r($json_decoded); //{"myarray":{"_id":"12345qwer","value":null,"provider":"foo"}}
Does anyone know why this happens and how can I get the actual url from the array cell?
I post a JSON from Android to PHP:
{"0":{"nome":"name","cf":"0101","address":"STREET 123"},"1":{"codice":"123","nome":"ACQUA","quantita":"3"},"2":{"codice":"123","nome":"ACQUA","quantita":"3"}}
In php i need to get user info always 0 (nome, cf and address) after this i need a while for getting dynamic element 1,2,3,4 etc etc (in while) always codice, nome and quantita but i have tried some code in php like:
$string = {"0":{"nome":"name","cf":"0101","address":"STREET 123"},"1":{"codice":"123","nome":"ACQUA","quantita":"3"},"2":{"codice":"123","nome":"ACQUA","quantita":"3"}};
$string = json_encode($string);
$nome = json_decode ($string, true);
echo $nome[0]->nome; //for single user info
but result is always white page
There are a few mistakes in the code...
$string = '{"0":{"nome":"name","cf":"0101","address":"STREET 123"},"1":{"codice":"123","nome":"ACQUA","quantita":"3"},"2":{"codice":"123","nome":"ACQUA","quantita":"3"}}';
//$string = json_encode($string);
$nome = json_decode ($string, true);
echo $nome[0]['nome']; //for single user info
The first line needs single quotes round it.
The json_encode() isn't needed as it's already JSON.
The last line needs to use ['nome'] as it's using arrays (by using true as the second parameter to json_decode())
I've been facing really hard times to read a bunch of data in JSON using json_decode() function in PHP. Basically I need to read basic data to display flights dates origins and destinations.
This is how it should be:
20 solutions found.
Solution# 1 Sale Price: BRL2919.54
Slice 0
TA 916 GRU 2015-06-16T06:15-03:00 LIM 2015-06-16T09:20-05:00
AV 962 LIM 2015-06-16T10:04-05:00 MIA 2015-06-16T16:48-04:00
And this is the JSON code: http://pastebin.com/dH16RriT
When I try to transform that and read it comes with NULL data.
$data = json_decode($results, true); // $results is a variable with
the JSON content from the URL above
echo $data['tripOption']['saleTotal']; // just an example
yes , you should use
print_r($a['trips']['tripOption'][0]['saleTotal'])
if you want to read the data inside that array,you can do as follows:
$data = json_decode($s,true);
foreach ($data['trips']['tripOption'] as $item){
print_r($item['saleTotal'] . "\n");
};
I have no problem to decode that data from the json structure you posted. Here is a trivial example script which dumped the decoded array structure:
<?php
$json = file_get_contents('./data.json');
$data = json_decode($json, true);
print_r($data);
About the specific data you try and fail to extract: you have to take care to use the correct "path" inside the data structure. This one works:
print_r($data['trips']['tripOption']);
Since that one is an array yo have to address a specific entry in there to get the output you expect:
echo $data['trips']['tripOption'][0]['saleTotal']; // just an example
$tempmoviename = "Battleship";
$omdburl = "http://www.omdbapi.com/?t=" . $tempmoviename;
$imdb_json = file_get_contents($omdburl);
$imdb_info = json_decode($imdb_json,true);
print ($imdb_info[0]->runtime[0]);
I can't get it to print the runtime of the movie. I can get it to print the actual website but not the information I need from the website.
In addition to that if I remove true
json_decode($imdb_json,true);
I get this error.
Fatal error: Cannot use object of type stdClass as array
How do I write this so it grabs the data from an array correctly? I also need to swap spaces and dashes in titles like Black Sheep, Black_Sheep to Black%20Sheep?
Do like this... Since you are converting the JSON to array (by passing true in the json_decode()), You need to access it like an array.
<?php
$tempmoviename = "Battleship";
$omdburl = "http://www.omdbapi.com/?t=" . $tempmoviename;
$imdb_json = file_get_contents($omdburl);
$imdb_info = json_decode($imdb_json,true);
echo $imdb_info['Runtime']; //"prints" 131 min
I have a php file that returns a single number (i.e. 360). How can I get that number to appear in my android textview. I am able to do this with arrays that look like this:
{"success":1,"message":"Post Available!","posts":[{"name":"John Smith","id":"1"}, ...]}
but how can I do it with just a single number.
Here is the php:
<?php
include('connect.php');
$gettotal=mysql_query("SELECT * FROM records");
totalrecords=mysql_num_rows($gettotal);
echo json_encode($totalrecords);
?>
Thanks.
A number is STILL going to be just a number after your json_encode() it.
$num = 42;
$json = json_encode($num);
echo "$num / $json"
will just output 42 / 42 - there will be no practical difference between the two.