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>";
Related
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)));
I want to decode the array value in web service.I need to decode 'CONTENT_VALUES' field which has name and value as its object and pass it in json.ID and USER_ID return correct value(separate field) but name and question returns null. I want to decode 'ques' => $datas->name, 'answer' => $datas->value.value of CONTENT_VALUES=[{"name":"radio","value":"","sur_id":"3","user_id":"admin#gmail.com","pagename":"question_response"},{"name":"true","value":""}]
$query1 = mysql_query("select * from `$prefix.response` where ID='$sur_id'");
while ($fetch = mysql_fetch_array($query1)) {
$content = $fetch['CONTENT_VALUES'];
$datas = json_decode($content);
$test[] = array('ID' => $fetch['ID'], 'USERID' => $fetch['USER_ID'], 'ques' => $datas->name, 'answer' => $datas->value);
}
Take a look at http://php.net/manual/en/function.json-decode.php.
This will describe how to convert a JSON string into a PHP object or array.
When you get stuck more with this you can fairly simple google "php decode json" and find plenty helpful resources.
Edit: Read the question too quick just before and understood it wrong, have you tried checking the return result of your $datas = json_decode($content) statement? Does CONTENT_VALUES actually contain JSON encoded data? Does the decoded object actually contain name and value?
May be you should set use mb_convert_encoding before decoding like this
$content = $fetch['CONTENT_VALUES'];
$content_value= mb_convert_encoding($content ,"UTF-8");
$datas = json_decode($content_value);
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.
Hello I have decoded a json string that I sent to my server and Im trying to get the values from him.
My problem is that I cant get the values from the inner arrays.
This is my code:
<?php
$post = file_get_contents('php://input');
$arrayBig = json_decode($post, true);
foreach ($arrayBig as $array)
{
$exercise = $array['exercise'];
$response["exercise"] = $exercise;
$response["array"] = $array;
echo json_encode($response);
}
?>
When I get the answer from my $response I get this values:
{"exercise":null,"array":[{"exercise":"foo","reps":"foo"}]}
Why is $array['exercise'] null if I can see that is not null in the array
Thanks.
From looking at the result of $response['array'], it looks like $array is actually this
[['exercise' => 'foo', 'reps' => 'foo']]
that is, an associative array nested within a numeric one. You should probably do some value checking before blindly assigning values but in the interest of brevity...
$exercise = $array[0]['exercise'];
Because of the [{...}] you are getting an array in an array when you decode your array key.
So:
$exercise = $array['exercise'];
Should be:
$exercise = $array[0]['exercise'];
See the example here.
I have a JSON result in PHP that looks like the following:
[{"attributes":{"type":"Manager__c","url":"/services/data/v23.0/sobjects/Manager__c/a03U00000015ay6IAA"},"Id":"a03U00000015ay6IAA","Name":"ManagerID-00003"},
{"attributes":{"type":"Manager__c","url":"/services/data/v23.0/sobjects/Manager__c/a03U00000015ZfJIAU"},"Id":"a03U00000015ZfJIAU","Name":"ManagerID-00001"},
{"attributes":{"type":"Manager__c","url":"/services/data/v23.0/sobjects/Manager__c/a03U00000015axwIAA"},"Id":"a03U00000015axwIAA","Name":"ManagerID-00002"}]
I want to eliminate the Attributes (which contains Type and URL) so that the JSON result is flattened. So I'd like it to look more like:
[{"Id":"a03U00000015ay6IAA","Name":"ManagerID-00003"},
{"Id":"a03U00000015ay6IAA","Name":"ManagerID-00003"},
{"Id":"a03U00000015ay6IAA","Name":"ManagerID-00003"}]
What is the best way in PHP to eliminate every instance of attributes?
//assign the orignal string to variable $json
$json = '[{"attributes":{"type":"Manager__c","url":"/services/data/v23.0/sobjects/Manager__c/a03U00000015ay6IAA"},"Id":"a03U00000015ay6IAA","Name":"ManagerID-00003"},{"attributes":{"type":"Manager__c","url":"/services/data/v23.0/sobjects/Manager__c/a03U00000015ZfJIAU"},"Id":"a03U00000015ZfJIAU","Name":"ManagerID-00001"},{"attributes":{"type":"Manager__c","url":"/services/data/v23.0/sobjects/Manager__c/a03U00000015axwIAA"},"Id":"a03U00000015axwIAA","Name":"ManagerID-00002"}]';
//decode the string with json_decode();
$decoded = json_decode($json);
//loop over the decoded array and populate array with Id and Name only
foreach($decoded as $d) $newarr[] = array('Id' => $d->Id, 'Name' => $d->Name);
//json encode the resulting array.
$finalJSON = json_encode($newarr);
Using json_decode(), array_map() and json_encode() should be easy enough:
function strip_arguments( $item){
$new_result = array(
'Id' => $item['Id'],
'Name' => $item['Name'],
)
return $new_result;
}
$array = json_decode( $input);
$array = array_map( 'strip_arguments', $array);
$input = json_encode( $array);
You of course may use unset() inside strip_arguments (instead of creating new array) but this will make sure any "new attribute" won't make it trough.
You can use return array(...) instead of declaring variable and chain operations to: json_encode(array_map( 'strip_arguments', json_decode( $input))); too :)
Using PHP's JSON's tools you can convert the JSON string into an array, from here use a simple foreach statement and REGEX to delete array entries.
Once this is done simply convert back into a JSON string using PHP functions and do with it as you please :-)
The functions I am thinking off are - json_decode, json_encode.
Thanks
Andrew