I'am passing values from java (android) to a web service (php) : the structure should be an array because the webservice take an array and make a serach in that array so how could I passing an array in $_POST :
$interet= $_POST['interet'];
// must be an array like this : $interet =array('piano','flute','chien');
NB/: the contain of the array is dynamic , it may have One or even ten value
You can create in Java json:
String mStringArray[] = { "piano", "flute", "chien" };
JSONArray mJSONArray = new JSONArray(Arrays.asList(mStringArray));
after send it to php server, and in php do this:
$array = json_decode($_POST, true);
Related
I have a json file:
$json='[{"Email":"myemail1#domain.com","Name":"company 1","Tel1":"xx-xx-xx","Adresse":"XXXXXX"},{"Email":"myemail2#domain.com","Name":"Company 2","Tel1":"xx-xx-xx","Adresse":"XXXXXX"}]';
and my forms post data in variable
vars="fname"=>"Ameur","lname"=>"KHIL"
"fname"=>"Marak","lname"=>"Cristo"
and I like to insert in between my json content with variable to get the final json like this:
$result='[{"Email":"myemail1#domain.com","Name":"company 1","vars":{"fname":"Ameur","lname":"KHIL","Tel1":"xx-xx-xx","Adresse":"XXXXXX"}},{"Email":"myemail2#domain.com","Name":"Company 2","vars":{"fname":"Marak","lname":"Cristo","Tel1":"xx-xx-xx","Adresse":"XXXXXX"}}]';
For this purpose you can use json_decode() to parse the JSON-String to a PHP-Object. Then you just set a new value vars to the given form values.
Parsing the JSON-String
$json = json_decode('[{"Email":"myemail1#domain.com","Name":"company 1","Tel1":"xx-xx-xx","Adresse":"XXXXXX"},{"Email":"myemail2#domain.com","Name":"Company 2","Tel1":"xx-xx-xx","Adresse":"XXXXXX"}]');
Adding the new vars value and removing the additional ones. This is just for the first entry but you can do the same for the other entry or even iterate through the array for multiple entries
$json[0]->vars = ["fname" => "Marak", "lname" => "Cristo", "Tel1" => $json[0]->Tel1,"Adresse" => $json[0]->Adresse];
unset($json[0]->Tel1);
unset($json[0]->Adresse);
And getting your result in a JSON-String
$result = json_encode($json);
I am developing an android application to store and invite user
contacts on the server.
the array I am having from android is JSON format.
$a='[
{"phone":"(785) 583-7086","name":"Website 22-2-16"},
{"phone":"(904) 136-2961","name":"Abhi"}
]';
I want these two attributes phone and name sent into an iteration in
PHP and store in respective columns like name and phone in MySQL
using PHP
Decode the json string using json_decode() function and loop through the decoded array using foreach loop, like this:
$a='[{"phone":"(785) 583-7086","name":"Website 22-2-16"}, {"phone":"(904) 136-2961","name":"Abhi"}]';
$decoded_json = json_decode($a, true);
foreach($decoded_json as $details){
// phone: $details['phone']
// name: $details['name']
// perform your INSERT operation
}
I am working on a project where a user has to fill up a form and can upload multiple documents. Everytime he uploads a document, he calls a webservice which returns a JSON file with id and type.
Since he can upload multiple documents so the webservice is called many times. When he completes his form and submits it, I need to send a json with the multiple id and type that I have received previously from JSON.
This is the JSON that I need to send to the server when the user finally submits the form:
"docls":[
{
"id":"123",
"ty":"101",
},
{
"id":"456",
"ty":"102",
}
],
How do I encode the JSON when I dont know how many files the user has uploaded?
P.S. I have already been able to get the response from the first JSON in the form of 'id' and 'ty'. Do I need to store it in array? If yes then how do I decode it into json?
$json_string = array(
'docls' => array (
'id' => '123',
'ty' => '101',
),
);
You're going to want to build the array with php then json_encode it when your ready to send it back to the server
// Create an array to hold the documents
$documents = [];
// push each document into the array as the user does their thing
$documents[$id] = $ty;
// When the user is finished
$json = json_encode($documents);
Well, you just have to use json_decode to convert json string you receive to php array, and json_encode to convert convert php array to json formatted string.
Check this example:
<?php
$json = '{"foo-bar": 12345}';
$obj = json_decode($json);
print $obj->{'foo-bar'}; // 12345
?>
more information here
I have a key/value pair passed from Javascript via $.post as data : user_id.
I've brought it into PHP with $data = $_POST['data'] and when I vardump() that I get {"id":"1"}" as expected. However, I'd like to just access the value of 1.
How would I do that?
It's just JSON. Use json_decode() to turn it into an object (or an array if you so choose) and then get the value of ID using standard object member variable access methods:
$data = json_decode($_POST['data']);
echo $data->id;
Demo
If you're using PHP 5.4+ (using array syntax and array dereferencing):
echo json_decode('{"id":"1"}', true)['id'];
Demo
You can also try this:
$data = json_decode($_POST['data'], true)['id']
I am using $.getJSON() to pass some data to the server side (PHP, Codeigniter) and using the return data to do some work. The data that I am sending over to the server is in the form of an array.
Problem: When an associative array is sent to the server, no result is received on server side. However, if a normal array with numerical indexes is sent, the data is received on server side. How can I send an array of data over to the server?
JS Code (Not Working)
boundary_encoded[0]['testA'] = 'test';
boundary_encoded[0]['testB'] = 'test1';
$.getJSON('./boundary_encoded_insert_into_db_ajax.php',
{boundary_encoded: boundary_encoded},
function(json) {
console.log(json);
});
JS Code (Works)
boundary_encoded[0][0] = 'test0';
boundary_encoded[0][1] = 'test1';
$.getJSON('./boundary_encoded_insert_into_db_ajax.php',
{boundary_encoded: boundary_encoded},
function(json) {
console.log(json);
});
PHP Code
$boundary_encoded = $_GET['boundary_encoded'];
print_r($_GET);
Error Msg
<b>Notice</b>: Undefined index: boundary_encoded in <b>C:\xampp\htdocs\test\boundary\boundary_encoded_insert_into_db_ajax.php</b> on line <b>11</b><br />
Array
(
)
Working Result
Array
(
[boundary_encoded] => Array
(
[0] => Array
(
[0] => test
[1] => test1
)
)
)
The reason this isn't working is because JavaScript does not support associative arrays. This assignment:
boundary_encoded[0]['testA'] = 'test';
appears to work in JS because you can assign a new property to any object, arrays included. However they won't be enumerated in a for loop.
Instead, you must use an object literal:
boundary_encoded[0] = {'testA':'test'};
You can then use JSON.stringify to convert boundary_encoded to a JSON string, send that to the server, and use PHP's json_decode() function to convert the string back into an array of objects.
I would suggest using converting the arrays to JSON. If you can't do so in PHP (using the json_encode function), here are a couple of JS equivalents:
http://phpjs.org/functions/json_encode:457
http://www.openjs.com/scripts/data/json_encode.php
In your getJSON call, use
{boundary_encoded: JSON.stringify(boundary_encoded)},
instead of
{boundary_encoded: boundary_encoded},