I have problem with a json, this json keeps updating, so the last entry will never be the same. This is the json:
{"channel":{"id":274304,"name":"Water","description":"Record water consumption.","latitude":"0.0","longitude":"0.0","field1":"Water","created_at":"2017-05-17T16:27:46Z","updated_at":"2017-05-19T01:01:07Z","last_entry_id":28},"feeds":[{"created_at":"2017-05-19T00:51:35Z","entry_id":1,"field1":"288"},{"created_at":"2017-05-19T00:51:50Z","entry_id":2,"field1":"304"}]}
I have something like this in my php code, where the variable $cool contains the decoded json.
$x= $cool->feeds[2]->field1;
But when the json updates, feeds[2] will not be the last entry. So i was thinking of using arrays to store all data. But i don't know how to do this. Can you help me?
If $cool->feeds is an array as I assume, you can use end to get its last item:
$x = end($cool->feeds)->field1;
If you want to get the last element, you also can use array_pop()
$x = array_pop($cool->feeds)->field1;
Related
So i am using this PHP code to create the json output, and I am having an issue where it’s creating an array of array with the info. I would like to get rid of one array and just display the list of API’s thats been used and number that has been used.
Looks as though the difference is you have...
"apis":[{"item_search":"0\n"},{"item_recommended":"0\n"}]
and want
"apis":{"item_search":"0\n","item_recommended":"0\n"}
If this is the case, you need to change the way you build the data from just adding new objects each time to setting the key values directly in the array...
$zone_1 = [];
foreach($zone_1_apis as $api_name ) {
$zone_1[substr($api_name, 0,-5)] = file_get_contents('keys/'.$_GET['key'].'/zone_1/'.$api_name);
}
You also need to do the same for $zone_2 as well.
It may also be good to use trim() round some of the values as they also seem to contain \n characters, so perhaps...
trim(file_get_contents('keys/'.$_GET['key'].'/zone_1/'.$api_name))
While retrieving values from array without index in PHP I am getting nothing.
From my database value is stored like ["SCHOOL"] and I want to get just SCHOOL from this.
It's not treated as array instead it's treated as a string.
What will be the solution if want to treat this as array and get value of that array.
My Code is as follows :
$stream_arr = $res_tbl['streams'];
which gives result as ["SCHOOL"]
and I want just SCHOOL from this.
I am using code as $stream = $stream_arr[0];
and I get '[' this as result.
If I assign manual value to $stream_arr = ["SCHOOL"],
above code works and returns SCHOOL as expected.
But for results from my database is not working where result is same as the one I manually assigned.
To eliminate braces from the string you can use below code.
$stream_arr = '["SCHOOL"]';
$temp=explode('"',$stream_arr);
$val=$temp[1];
echo $val; //it will dispaly 'SCHOOL'.
But check the insert query(from the place you store data into db) that why it is storing with braces? if it is done by you as required then proceed else first fix that issue instead of eliminating braces in php code.
not sure what I'm doing wrong here but i have my Json string and I'm trying to get the values to print out in the IF statement
i.e:
63,52,55
here is my code:
$jayjay = '{"items":{"cID":"63","qty":"2"},"items":{"cID":"52","qty":"1"},"items":{"cID":"55","qty":"1"}}';
echo $jayjay;
$obj = json_decode($jayjay, TRUE);
for($i=0; $i<=count($obj['items']); $i++) {
echo $obj['items'][$i]['cID'];
echo ",";
}
but the output is just blank and i cant figure out why
Any help would be appreciated.
Thank you
The problem is that you have key "items" multiple time in your JSON which is wrong. Please note that JSON key must be unique for well formed JSON.
Basically there is no error if you use more than one key with the same name, but in JSON, the last key with the same name is the one that is gonna be used. In your case, the key "items" would be better to contain an array of objects as it's value:
{'items' : [{"cID":"63","qty":"2"}, {"cID":"52","qty":"1"}, {"..." : "..."}]}
As Hamish stated in his comment, you can't have json as you've supplied. The keys (which is items in this case) are going to overwrite each other until the last key.
What I suggest is if you create an array like this:
{"items":[{"cID":"63","qty":"2"}, {"cID":"52","qty":"1"},.....]}
Allowing you to itterate as you require.
Meaning you can simply loop as you require:
$items = json_decode($json, true);
foreach($items as $thing){
echo $thing['cID'];
echo ",";
}
As has already been pointed out, your JSON data is malformed.
But since Stack Overflow is a rather inefficient method of syntax checking your data, I would suggest using a JSON linter (such as this one) on static JSON data in the future. Or at the very least, checking the return value of json_last_error after parsing to look for potential problems at least when testing or debugging.
So I am working with PHP to pass a PHP array over a jQuery Ajax request to another PHP page. This is quite the task. For some reason, my json_encode is returning an array instead of a string, I am not quite sure why. Here is my PHP code:
$new_spreadsheet = nl2br($_POST['spreadsheet']);
$new_spreadsheet = explode('<br />', $new_spreadsheet);
array_shift($new_spreadsheet);
$new_spreadsheet = array_values($new_spreadsheet);
echo json_encode($new_spreadsheet);
I would show you the output, but it is really long. Basically this is outputting a PHP array which consists of each row on the spreadsheet. This is what I want to have, but the problem is that I don't want the quotes and everything in the array. I am pretty sure I need to run json_decode, but when I do that my code returns an error saying that the parameter needs to be a string. I can tell something is not right, but am not quite sure what I need to change. I would appreciate any advice.
Update: At this point, when I try to loop through the array and print each value, the first array index is equal to a double quote like so: ". There are double quotes in random values throughout the area. I am not quite sure about what is causing this.
If I echo the rows from within the json_encoded PHP array onto the console, I get the following output:
"
correct value
correct value
correct value
"
You're using JSON, which means you have to adhere to somewhat more stringent syntax rules than Javascript's. e.g.
<?php
$arr = array('This' => 'is', 'an' => 'array in php');
echo json_encode($array);
?>
output:
{"This":"is","an":"array in PHP"}
There is NO way to avoid getting quotes on the values, as they're a fundamental requirement of JSON (and Javascript). If you don't want quotes, then don't use JSON.
try only br.
$new_spreadsheet = explode("<br>", $new_spreadsheet);
It will work. and json_enode can never return an array. try var_dump and check.Also make sure before you post, use htmlspecialcharacters.
I'm new to programming and I'm tackling arrays. I thought I had multi-dimensional arrays figured out but I guess I don't. Here's the code I'm working on:
$loopcounter = 0;
while ($myrow = mysql_fetch_array($results)) {
//...other stuff happens...
$allminmax[$loopcounter][] = array("$myrow[3]","$currentcoltype","$tempmin","$tempmax");
$loopcounter++;
}
This chunk of code is supposed to create an array of four values ($myrow[3], currentcoltype, tempmin, tempmax) and insert it into another array every time the loop goes through. When I do this:
echo implode($allminmax);
I get:
ArrayArrayArrayArrayArrayArrayArrayArrayArray
Do I need to implode each array before it goes into the master array? I really want to be able to do something like $allminmax[0][4] and get the $tempmax for the first row. When I try this now nothing happens. Thanks -- any help is appreciated!
It looks like you should either use [$loopcounter] or [], but not both. You also should drop the quotes. They're unnecessary and in the case of "$myrow[3]" they interfere with the variable interpolation.
$allminmax[] = array($myrow[3], $currentcoltype, $tempmin, $tempmax);
By the way, arrays are zero-indexed, so to get $tempmax for the first row it'd be $allminmax[0][3] not $allminmax[0][4].
Also, a better way to display the contents of your array is with print_r or var_dump. These will display arrays within arrays while a simple echo will not.
var_dump($allminmax);