This question already has answers here:
Remove double quote in json_encode()
(6 answers)
Closed 3 years ago.
code:
<?php
if(!isset($candidate_id))
{
header("location:".base_url()."login");
}
$this->db->select('event,event_title,description,s_date');
$this->db->from('event');
$this->db->where('candidate_id',$cid);
$this->db->order_by('s_date','desc');
$query = $this->db->get();
if($query->num_rows() > 0)
{
$result = $query->result_array();
$record = array();
foreach($result as $row)
{
$record[] = $row;
}
echo json_encode($record,JSON_NUMERIC_CHECK);
}
else
{
$this->session->set_flashdata('no_event',"<p>No event Added</p>");
}
?>
current output:
[{"event":"2019-03-06","event_title":"meeting","description":"meeting with xyz","s_date":"2019-03-04"}]
expected output:
[{event:"2019-03-06",event_title:"meeting",description:"meeting with xyz",s_date:"2019-03-04"}]
I am creating a simple JSON API using json_encode() function. Now, API I created successfully but the output is unexpected as I mention above. Now, What I actually want I also mention above. So, How can get I expected output? Please help me.
Thank You
The problem is, if you remove the quotes from the key, it's no longer JSON. I'm assuming you want it similar to a JavaScript object, but that should be done on the client side, using JSON.parse().
ie:
var apiResponse = '[{"event":"2019-03-06","event_title":"meeting","description":"meeting with xyz","s_date":"2019-03-04"}]';
var json = JSON.parse(apiResponse);
console.log(json);
console.log(json[0].description);
Try this
$array_final = preg_replace('/"([a-zA-Z_]+[a-zA-Z0-9_]*)":/','$1:',json_encode($you-array));
Output
{event:"2019-03-06",event_title:"meeting",description:"meeting with xyz",s_date:"2019-03-04"}
Related
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:
How can I access an array/object?
(6 answers)
Closed 11 months ago.
i got this json encoded data being sent to me.
can u tell me how to get each of the individual elements ?
Something like:
$ticket
$customer
$user
{"ticket":{"id":"10909446","number":"152"},"customer":{"id":"3909381","fname":"","lname":"","email":"me#site.com","emails":["me#site.com"]},"user":{"fname":"Test","lname":"Me","id":17396,"role":"admin"}}
this is a basic view on how my code runs.
$ret = array('html' => '');
$data = json_decode($data , true);
$ret['html'] = '<ul><li>'.$data->ticket->number.'</li></ul>';
echo json_encode($ret);
exit;
it only prints the circle from the li tags.
To clarify #Cthulhu's answer :
$test = '{"ticket":{"id":"10909446","number":"152"},"customer":{"id":"3909381","fname":"","lname":"","email":"me#site.com","emails":["me#site.com"]},"user":{"fname":"Test","lname":"Me","id":17396,"role":"admin"}}';
$data = json_decode($test);
echo $data->ticket->id;
outputs
10909446
json_decode make the JSON into a stdClass object, and then you can access the values as that.
$data = json_decode($test);
$ret = array();
$ret['html']='<ul><li>'.$data->ticket->number.'</li></ul>';
return
json_encode($ret);
will return
{"html":"<ul><li>152<\/li><\/ul>"}
json_decode is the answer for you.
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"];
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);