This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
encode json using php?
$hello_world = $this->session->all_userdata();
foreach($hello_world as $key=>$product_id)
{
$query['products'] = $this->Global_products->globalFindProductsViewed($product_id);
foreach($query['products'] as $product)
{
$ryan[] = $product->name;
}
}
foreach($ryan as $r)
{
echo json_encode(array($r));
}
The output then looks like this:
["Alpine 50W x 4 AppleĀ® iPodĀ®-Ready In-Dash CD Deck"]
I know I cant access this with JavaScript.
Can someone suggest how I can make this work?
JSON encoding every array element separately doesn't make sense.
Remove the foreach, and just do a
echo json_encode($ryan);
Related
This question already has answers here:
PHP. Is it possible to use array_column with an array of objects
(5 answers)
How to access object properties with names like integers or invalid property names?
(7 answers)
Closed 1 year ago.
Hi I'm currently trying to do an API request. The API sends out a json request like this:
[
{
"name": "Test 1"
"yes-or-no": "yes"
},
{
"name": "Test 2"
"yes-or-no": "no"
}
]
My question is, how do I select one of the yes-or-no to echo in the website? I tried doing this:
<?php
$status = json_decode(file_get_contents('url to the json file'));
// Display message AKA uptime.
foreach ($status->yes-or-no as $answer) {
echo $answer.'<br />';
}
?>
But didn't work.
I'm sorry if I got some terms wrong, since I'm pretty new to coding APIs like this.
EDIT: Please see the answer below. It works but now my question is: How do I only display one of them? Instead of both of them displaying at the same time.
I'm not really sure what you are trying to do, but maybe i can shed some light into the question:
$status = json_decode(file_get_contents('url to the json file'), true);
Add ", true" this will make your $status an array instead of an object.
foreach ($status as $answer) {
echo $answer['yes-or-no'].'<br />'; //output yes or no
echo $answer['name'].'<br />'; //output test 1 or test 2
}
Try something like this:
<?php
$statuses = json_decode(file_get_contents('url to the json file'));
foreach ($statuses as $status) {
echo $status->{'yes-or-no'};
}
?>
This question already has answers here:
PHP json_encode encoding numbers as strings
(18 answers)
Closed 2 years ago.
I get data php sql after i print from json its but i want dont print a item in ""
i want dont print id in ""
require('config.php');
$sql="SELECT * FROM file";
$result=$conn->query($sql);
foreach($result as $rows){
$recordfil=array();
$recordfil['id']=$rows['idfile'];
// $recordfil['file']=$rows['filef'];
$nnfile[]=$recordfil;
}
$filegen=$nnfile;
$arrayovp=[
'ovpn_file'=>
$filegen
];
echo json_encode($arrayovp);
?>
It output :
{"ovpn_file":[{"id":"36"},{"id":"35"}]}
I want only 36 or 35 without ""
PHP supports automatically encoding numeric strings as numbers using the JSON_NUMERIC_CHECK option, which might be easier than explicitly type-casting all relevant fields.
Try
echo json_encode($arrayovp, JSON_NUMERIC_CHECK);
# {"ovpn_file":[{"id":36},{"id":35}]}
Demo here: https://3v4l.org/ANdUM
This question already has an answer here:
How to extract and access data from JSON with PHP?
(1 answer)
Closed 7 years ago.
How do I return only all the urls item "webformatURL" using json decode?
{
"totalHits":500,
"hits":[
{
"previewHeight":84,
"likes":37,
"favorites":42,
"tags":"yellow, natural, flower",
"webformatHeight":360,
"views":11218,
"webformatWidth":640,
"previewWidth":150,
"comments":8,
"downloads":6502,
"pageURL":"https://example.com/en/yellow-natural-flower-715540/",
"previewURL":"https://example.com/static/uploads/photo/2015/04/10/00/41/yellow-715540_150.jpg",
"webformatURL":"https://example.com/get/ee34b40a2cf41c2ad65a5854e4484e90e371ffd41db413419cf3c271a6_640.jpg",
"imageWidth":3020,
"user_id":916237,
"user":"916237",
"type":"photo",
"id":715540,
"userImageURL":"https://example.com/static/uploads/user/2015/04/07/14-10-15-590_250x250.jpg",
"imageHeight":1703
},
],
"total":7839
}
I try:
$test= json_decode($json);
$result= $test->webformatURL;
Error: warning-undefined-property-stdclass::webformatURL
I've read the manual but I doubt then I would see an example in practice
json_decode
Thanks for any help
Did you mean?
$result = $test->hits[0]->webformatURL;
If you want to extract only this field from the object, you can do:
$urls = [];
foreach($test->hits as $hit) {
$urls[] = $hit->webformatURL;
}
var_dupm($urls);
This question already has answers here:
Read Json/Array string in Php
(2 answers)
Closed 8 years ago.
In fact Im having some troubles with php script that i have made recently. The problem is that I can't get data from a json file damo.json using php. This is the code of the json file:
{ "checkouts":[
{
"billing_address":{
"country":"Italy",
"first_name":"christian"
}
},
{
"billing_address":{
"country":"Italy",
"first_name":"christian"
}
}
]
}
i want to get the first_name record. Is it possible using php ?
this is the php code:
<?php
$data = file_get_contents('demo.json');
$obj = json_decode($data);
foreach ($obj->billing_address as $result)
{
echo $result->name;
}
?>
After you fix your syntax as noted above load the file and use json_decode with first parameter the json and second parameter true for associative array.
$data = file_get_contents( "demo.json" );
$data = json_decode($data, true);
var_dump($data);//you have assoc array
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to access an object property with a minus-sign?
I am trying to parse geo:point / geo:lat but getting nowhere fast. Here's the code I have so far
$content = get_data('http://ws.audioscrobbler.com/2.0/?method=geo.getevents&location=united+kingdom&api_key=XXX&format=json&limit=10000&festivalsonly=1');
$LFMdata = json_decode($content);
foreach ($LFMdata->events->event as $event) {
$venue_lat = $event->venue->location["geo:point"]["geo:lat"];
$venue_long = $event->venue->location["geo:point"]["geo:long"];
The JSON will contain something like
"geo:point": {
"geo:lat": "52.7352",
"geo:long": "-1.695392"
}
Anyone got any idea? Seen examples in JavaScript but not PHP
You can use
$venue_lat = $event->venue->location->{"geo:point"}->{"geo:lat"};
Or make it return everything like an associative array (second param of json_decode set to true):
$LFMdata = json_decode($content, true);
foreach ($LFMdata["events"]["event"] as $event) {
$venue_lat = $event["venue"]["location"]["geo:point"]["geo:lat"];
$venue_long = $event["venue"]["location"]["geo:point"]["geo:long"];