Read out a json file php - php

Hi I have this json code
{"points":[ {"10":"10"}, {"1":"1"} ]}
and this is my php code
$pointsfirst = $row['points'];
$points = json_decode($pointsfirst,true);
$getit = $points['points'][1]['10'];
echo $getit;
$row['points'] is from my database where I have stored the json
and I keep getting this error
Notice: Undefined offset: 10 in
/Applications/MAMP/htdocs/projectg/getpointsapi.php on line 46
what am I doing wrong ??

Right way is:
$getit = $points['points'][0][10];
echo $getit;

Try
$getit = $points['points']['1']['10'];
with the quotas! instead...

Arrays in PHP start with offset 0, not 1. This means you should probably use $getit = $points['points'][0]['10']; instead.

Related

php json decode/parsing poloniex API

I am trying to run a php script in terminal to get a organized reply of cryptocurrency data. the full JSON array can be viewed at https://poloniex.com/public?command=returnTicker. The nested array isnt stacked with [] and parsing this format is becoming troublesome.
here is the code i used:
$fgc = json_decode(file_get_contents("https://poloniex.com/public?command=returnTicker"), true);
echo "Last Price: $".$fgc["last"]." Ask: ".$fgc["lowestAsk"]." Bid: ".$fgc["highestBid"];
echo PHP_EOL;
This is the error it gives me:
PHP Notice: Undefined index:
I have a feeling i need to define each coin however im not sure how to parse it.
other code i have tried:
$fgc = json_decode(file_get_contents("https://poloniex.com/public?command=returnTicker"), true);
$data = $fgc["BTC_BCN"];
foreach($data as $details){
echo "".$details["last"]." ".$details["lowestAsk"];
echo " ".$details["highestBid"];
echo PHP_EOL;
}
The error for this code is:
PHP Warning: Illegal string offset
Please help me understand how to parse this format. Thank you.

php json_decode not working on json array

I have been searching the internet for hours now, and i can't figure it out.
<?php
$json = '{"pages":[{"name": "Page1","inputs":[{"title": "Catagory","name": "catagory","type": "radio","options":[{"name": "Paper","value": "paper"}{"name": "Letter","value": "letter"}]}{"title": "Title","name": "title","type": "text"}{"title": "File","name": "file","type": "file","fileName": "?pages[0].inputs[0]"}{"title": "Submit","type": "submit"}]}]}';
$result = json_decode($json, true);
var_dump($result);
echo $result['pages'][0]['name'];
echo $pages[0]['name'];
?>
Im just simply trying to parse some json but the website says this:
NULL
Notice: Undefined variable: pages in C:\Users\hazzj\Desktop\Stuff\Apache-Server\htdocs\WMS\Author\submit\test.php on line 7
You'd missed out commas between strings {}. Use this modified $json variable:
$json = '{"pages":[{"name": "Page1","inputs":[{"title": "Catagory","name": "catagory","type": "radio","options":[{"name": "Paper","value": "paper"},{"name": "Letter","value": "letter"}]},{"title": "Title","name": "title","type": "text"},{"title": "File","name": "file","type": "file","fileName": "?pages[0].inputs[0]"},{"title": "Submit","type": "submit"}]}]}';
$result = json_decode($json, true);
echo $result['pages'][0]['name']; // Output: Page1
echo $pages[0]['name']; // Not sure what this $pages variable is

Can't read a string of JSON

I have this JSON (is from Steam Market):
{"success":true,"lowest_price":"$5.79","volume":"2,932","median_price":"$5.79"}
And I have this code:
$urlm = 'http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name='.$name;
$market = json_decode(file_get_contents($urlm), true);
echo $market['lowest_price'];
But when I try it, they give me an error:
Notice: Undefined index: lowest_price
What is wrong? Because I have an other JSON with the same method and works perfect.
You should urlencode your varaiable:
$urlm = 'http://steamcommunity.com/market/priceoverview/?currency=1&appid=730&market_hash_name='. urlencode($name);

PHP Array breaking

This works on my test environment, but on my live server there is a later version of PHP which is throwing up an error and breaking my program
The code is
$oldFile = fopen("D:/ftpfolderreport/report/" . $last_file, "r");
while(!feof($oldFile))
{
$buffler = fgets($oldFile);
$bufflerArray = explode(",", $buffler);
$key = $bufflerArray[0];
$oldFileArray[$key] = $bufflerArray[1];
}
fclose($oldFile);
This line:
$oldFileArray[$key] = $bufflerArray[1];
Is throwing out this error
Notice: Undefined offset: 1 in D:\apps\wamp\www\Compliance2\compareFtpReports.php on line 57
I think this is to do with how I'm adding the $key variable inside the argument. I've tried it as ["$key"] and ['$key'] but it doesn't like it.
I have tried defining the key variable earlier in the program but still doesn't like it. I've been searching around online but can't find anything of help. Anyone any ideas?
Thanks,
Stephen.
add checks for empty
if (!empty($bufflerArray[1])) {
$key = $bufflerArray[0];
$oldFileArray[$key] = $bufflerArray[1];
}

Opencart Session Variable Errors

I'm having trouble sending data via sessions because I'm getting errors of undefined variable while defining the variable in controller/checkout/shipping_address.php under validate() function. (checkout/shipping_address/validate).
$this->session->data['ship_date'] = $this->request->post['ship_date']; //<- line 102
In controller/checkout/shipping_method
$ship_date = $this->session->data['ship_date'];
if(empty($ship_date)) echo "var empty";
$ship_date = explode("-", $ship_date);
$ship_date = $ship_date[0] . "/" . $ship_date[1] . "/" . $ship_date[2];
and then I do
$quote = $this->{'model_shipping_' . $result['code']}->getQuote($shipping_address, $ship_date);
Also yes, in model/shipping/fedex.php I allow usage of $ship_date parameter. Yet after that I get.
Invalid JSON: Notice: Undefined index: ship_date in
/var/www/catalog/controller/checkout/shipping_address.php on
line 102[] parsererror Notice: Undefined index:
ship_date in
/var/www/catalog/controller/checkout/shipping_address.php on
line 102[]
You should debug the arrays $this->session->data and $this->request->post.
The reason you see those errors is that there is no index ship_date in $this->session->data and in $this->request->post. So you get a Notice: Undefined index:.
Because of the notices that are printed your afterwards outputted json becomes invalid.
Actually, OpenCart only talks through JSON. So adding this will help.
$JSONarray = array("date" => $this->request->post['ship_date']);
$this->session->data['ship_date'] = json_encode($JSONarray);
When you want to use it,
$JSONarray = $this->session->data['ship_date'];
$arr = json_decode($JSONarray, TRUE);
$Value = $arr['ship_date'];
We have to make the data JSON and then send it

Categories