I want to decode the value which I fetched. I fetched and concat ',' to the value $album.=$fet_pic['CONTENT_VALUE'].','; I want to decode the value and send it to jsondata. Now 'albpic' return null.I got the json value as shown in image.
$alb = array();
$get_alb = mysql_query("select * from album");
while($fet_alb = mysql_fetch_array($get_alb)) {
$id=$fet_alb['ID'];
$alid=$fet_alb['CONTENT_VALUE'];
$albpic=mysql_query("select * from album_details where SUB_ID='$id'");
$album='';
while($fet_pic=mysql_fetch_array($albpic))
{
$album.=$fet_pic['CONTENT_VALUE'].',';
}
// $album = substr($album,0,-1);
$alb[] = array_merge(array('id' => $id),json_decode($fet_alb['CONTENT_VALUE'], true),array('albpics'=>json_decode($album)));
}
echo json_encode($alb);
$album={"media_type":"image/png","content_type":"alb_detail","website_id":"571710720","last_modified_date":"2015-11-23T05:27:03.806Z","thumnail_pic_loc":"link.png","large_pic_loc":"link.png","filter_type":"image/png","photodescription":"No Description","pic_id":"zhadb"},{"media_type":"image/png","content_type":"alb_detail","website_id":"571710720","last_modified_date":"2015-11-23T05:27:03.806Z","thumnail_pic_loc":"link.png","large_pic_loc":"link.png","filter_type":"image/png","photodescription":"No Description","pic_id":"zhadb"},{"media_type":"image/video","content_type":"alb_info","website_id":"571710720","last_modified_date":"2015-11-23T05:27:03.806Z","thumnail_pic_loc":"http://img.youtube.com/vi/fdgd/default.jpg","large_pic_loc":"http://img.youtube.com/vi/fdgd/hqdefault.jpg","filter_type":"image/video","videoname":"fdgd","photodescription":"dfgdfg","pic_id":"kg5k4"}
To use json_decode() the parameter you are passing should be in json_encoded format.
change this
$alb[] = array_merge(array('id' => $id),json_decode($fet_alb['CONTENT_VALUE'], true),array('albpics'=>json_decode($album)));
to
$alb[] = array_merge(array('id' => $id),$fet_alb['CONTENT_VALUE'], ,array('albpics'=>$album));
If $fet_alb['CONTENT_VALUE'] this is json encoded value, use
$arr = json_decode($fet_alb['CONTENT_VALUE']);
$arr1 = imlpode(",",$arr);// this will give comma separated values.
Your $album variable does not return proper json values.
So, try to replace with below lines. Hope it will works for you.
$alb[] = array_merge(array('id' => $id),json_decode($fet_alb['CONTENT_VALUE'], true),array('albpics'=>json_decode("[".$album."]",true)));
Related
I have a problem here, I want to change the query string that I received to the json form as follows
{"id":"89"}
here I try to use json_encode but the one it produces is
""?id=89""
here is my code
$coba = str_replace($request->url(), '',$request->fullUrl());
if (empty($coba)) {
$url = "{}";
} else {
$url = $coba;
}
$json_body = json_encode($url);
there I also want to change if there is no query string then the result is {}
This should do it for you for both conditions:
json_encode($request->query(), JSON_FORCE_OBJECT);
PHP Manual - JSON Functions - json_encode
Laravel 5.8 Docs - Requests - Retrieving Input - Retrieving Input From The Query String query
//get Parameters
$array = [
'id' => 20,
'name' => 'john',
];
//getting the current url from Request
$baseUrl = $request->url();
//we are encoding the array because
//u said that the get parms in json format
//so that ...
$json = json_encode($array);
//now building the query based on the data
//from json by decoding it
$query = (http_build_query(json_decode($json)));
//at the end of the url you need '?' mark so...
$url = \Illuminate\Support\Str::finish($baseUrl,'?');
//now Merging it
$fullUrl = $url.$query;
dump($fullUrl);
For any issues kindly comment
I have this array:
$users = array();
// Finally, loop over the results
foreach( $select as $email => $ip )
{
$users[$email] = $ip;
}
Now i want to pass this array to ajax and print in another script:
$html="<center>Check users for spam</center>";
mcheck.php
echo $_POST['users'];
This code does not work. How can i do this?
As Sergiu Costas pointer out, the best way is to JSON encode it and then decode, but you can also try:
$html="<center>Check users for spam</center>";
If your $users array is:
$users = array( array('email' => 'a#email.com'), array('email' => 'b#email.com')));
then http_build_query('users' => $users) will generate:
users%5B0%5D%5Bemail%5D=a%40email.com&users%5B1%5D%5Bemail%5D=b%40email.com
which is the url-encoded version of:
users[0][email]=a#email.com&users[1][email]=b#email.com
which will be decoded back to array in mcheck.php
The Best way is to encode array into JSON format.
In PHP:
$users_json = json_encode($users);
// To decode in javascript
var obj = JSON.parse(json_str);
if u want to pass php array to js you can json_encode the array to do that.
$html="<center>Check users for spam</center>";
I am trying to create JSON from a comma delimited string.Now my code is like this
$sql="select Pri_name from nri_privilege";
$getvalue=mysqli_query($mysqli,$sql);
while($result=mysqli_fetch_assoc($getvalue))
{
$getallresults[]=$result;
}
foreach($getallresults as $rereult)
{
print_r($rereult);
name =$rereult['Pri_name'];
name .=$name .',';
//echo $name;
//echo json_encode($name);
}
I am fetching data from database and loop it inside a foreach. How to create JSON from a comma delimited string?
Based on the title of your post this would do what you described.
$array = ["foo", "bar", "bat"]; //some array form e.g. db
$glued = implode(",", $array); //implode it with ,
$json = json_encode($glued); //and json encode it
What I think you really want is this:
$array = ["foo", "bar", "bat"]; //some array form e.g. db
$json = json_encode($array); //json encode the array without step 2.
Guess you want to send the json encoded array over the wire to somewhere else. In this case no need for any implode or explode.
Try This
$sql="select Pri_name from nri_privilege";
$getvalue=mysqli_query($mysqli,$sql);
while($result=mysqli_fetch_assoc($getvalue))
{
$getallresults[]=$result['Pri_name'];
}
echo json_encode($getallresults);
You better not append , manually. Try this,
$names = array();
foreach($getallresults as $rereult){
$names[] = $rereult['Pri_name'];
}
then,
$comma_seperated_list = implode(", ", names);
If you want the above string to be json encoded,
json_encode($comma_seperated_list);
Otherwise you could encode the whole name array like,
json_encode($names);
Hope this helps you!
I'm working with Laravel 5 right now and I have the following problem. I've got response from DB query:
[{"id":1}]
and I want to take out 1 as int or string. Any ideas?
I've tried to solve this like follows:
$json = (DB query);
$data = json_decode($json);
$final = $data[0]->id;
and response is :
json_decode() expects parameter 1 to be string, array given
This is all you need.
$response = json_decode($response); // Decode the JSON
$string = $response[0]->id; // Save the value of id var
As you say, the string you have is in JSON format, so you need to use json_decode to access it in PHP.
The square brackets refer to an array, and the braces refer to an object within that array, so what you're looking for is the id value of the first element (i.e. element 0) in the array.
<?php
$json = '[{"id":1}]';
$data = json_decode($json);
echo $data[0]->id;
// 1
Try this
$json_array = '[{"id":1}]';
$data = json_decode($json_array);
print_r($data); // To display the result array
You just need to decode it, if I'm not misunderstanding your questions.
$json = '[{"id":1}]';
$decodedObject = json_decode($json);
Then you can loop through the aray and do something with your data:
foreach($decodedObject as $key => $value) {
$id = $value->id;
}
If you're using Laravel, though, why not use Eloquent models?
I have three arrays example: $array1,$array2,$array3.
I want to insert the array2 and array3 in two different columns in the same table.
can i do that?
here is the code below that i am trying but it does not work for me i am doing in codeigniter:
controller:
$athletes_id = $this->input->post('athletes'); // array 1
$fields_id = $this->input->post('fields_id'); // array 2
$athlete_score = $this->input->post('athlete_score'); // array 3
$id = array();
foreach($athlete_score as $row){
$additional_data = array(
'test_reports_id' => $test_report_id,
'score' => $row,
);
$id[] = $this->test_model->save_test_reports_details($additional_data);
}
for($j=0;$j<count($fields_id);$j++){
$data2 = array('fields_id' => $fields_id[$j]);
$this->test_model->update_test_reports_details($id,$data2);
}
Model :
public function update_test_reports_details($id,$data2){
$this->db->where_in('id',$id);
$this->db->update('test_reports_details',$data2);
}
Just serialize the arrays.
$data2 = serialize($array2); // or whatever array you want to store
Then to retrieve with unserialize
$array2 = unserialize($data2);//or the row index
To store array in database you have to serialize it.
Try :
public function update_test_reports_details($id,$data2){
$this->db->where_in('id',$id);
$this->db->update('test_reports_details',serialize($data2));
}
To unserialize it when you get it from the database you have to use unserialize()
You can serialize as suggested in other answers, however I personally prefer to json_encode the array.
$data = json_encode($array);
And when you are reading in the model you should decode the data:
$array = json_decode($data, true); // set second argument to true to get an associative array
Using JSON has the advantage of better readability while still in the DB. This might not seem like much, but it can really help in some cases.