how do i store android json array in php databse? - php

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
}

Related

Add json object to json data fetched from database in php

In database JSON data is stored in below way:
{"name":"abc","score":5}
Now everytime I want to add json object with different name and score to same json data as :
{"name":"abc","score":5},{"name":"efg","score":8},{"name":"xyz","score":6}
Right now I have tried this code
$database_data = $row['JSONstring']; //fetched json string from database
$json_decode_data = json_decode($database);
array_push($json_decode_data, $new_json_string); //data to append to database data
$updated_json_data = json_encode($json_decode_data); // at last encode the update json data
But this don't work and throws a warning : Parameter 1 is an object.
How do solve it ?
Now with all the answer and comments given here I got the required answer. So I appreciate all your help.
In my question starting, the first JSON String format is invalid:
{"name":"abc","score":5}
Instead of this, it would be :
[{"name":"abc","score":5}]
Required JSON data's format would be:
[{"name":"abc","score":5},{"name":"efg","score":8},{"name":"xyz","score":6}]
And this is achieved by array_merge() function as follows:
Code to append json object to json object string
//fetched json string from database
ie.[{"name":"abc","score":5}]
$database_data = $row['JSONstring'];
//decode both string the database data string and the new one which is to be merged
$json_decode_database_data = json_decode($database_data, true);
$json_decode_new_data = json_decode($database_data, true);
//merge both the decoded array by array_merge()
$required_json_data = array_merge($json_decode_data, $new_json_string);
//at last encode required json data
$updated_json_data = json_encode($required_json_data);
// You will get required json data
[{"name":"abc","score":5},{"name":"efg","score":8},{"name":"xyz","score":6}]

How to encoding multiple json object in php

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

save value taken en $_POST in an array

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);

Query and decode json form mysql database

I have this JSON encoded code in my mysql database:
{"Suggestion":{"Title":"Casinos","Text":"maybe it will be good if its there casinos "},"ID":6,"VoteNo":[],"Status":"Voting","Player":{"SteamID":"STEAM_0:1:36988062","Name":"Pepi"},"Approved":{"Name":"Nido Johnson","Is":true,"SteamID":"STEAM_0:0:47457253"},"VoteYes":{"1":"STEAM_0:0:56939043","2":"STEAM_0:0:55948188","3":"STEAM_0:1:25856984","4":"STEAM_0:1:40894071"}}
And i want to query and decode it to echo it at my website.
You have to use a php "json_decode()" function to decode a json encoded data.
Basically json_decode() function converts JSON data to a PHP array.
Syntax: json_decode( data, dataTypeBoolean, depth, options )
data : - The json data that you want to decode in PHP.
dataTypeBoolean(Optional) :- boolean that makes the function return a PHP Associative Array if set to "true", or return a PHP stdClass object if you omit this parameter or set it to "false". Both data types can be accessed like an array and use array based PHP loops for parsing.
depth :- Optional recursion limit. Use an integer as the value for this parameter.
options :- Optional JSON_BIGINT_AS_STRING parameter.
Now Comes to your Code
$json_string = '{"Suggestion":{"Title":"Casinos","Text":"maybe it will be good if its there casinos "},"ID":6,"VoteNo":[],"Status":"Voting","Player":{"SteamID":"STEAM_0:1:36988062","Name":"Pepi"},"Approved":{"Name":"Nido Johnson","Is":true,"SteamID":"STEAM_0:0:47457253"},"VoteYes":{"1":"STEAM_0:0:56939043","2":"STEAM_0:0:55948188","3":"STEAM_0:1:25856984","4":"STEAM_0:1:40894071"}}';
Assign a valid json data to a variable $json_string within single quot's ('') as
json string already have double quots.
// here i am decoding a json string by using a php 'json_decode' function, as mentioned above & passing a true parameter to get a PHP associative array otherwise it will bydefault return a PHP std class objecy array.
/ just can check here your encoded array data.
// echo '<pre>';
// print_r($json_decoded_data);
// loop to extract data from an array
foreach ($json_decoded_data as $key => $value) {
echo "$key <br/>";
foreach($value as $k=>$data)
{
echo "$k | $data <br/>";
}
}

how do json decode in php?

I have the following json string and I want to retrieve just the email address from it. How do I do it in php?
[{"username":"23441","username":"vanthien","phone":"0293029230"},{"username":"23442","username":"hoangtung","phone":"0599799930"},
{"username":"23443","username":"thanhtung","phone":"069929230"},
{"username":"23444","username":"redlight","phone":"0293299230"},]
Thanks.
PHP's JSON functions are documented here: http://us3.php.net/json
The json_decode() function may be especially useful: http://us3.php.net/manual/en/function.json-decode.php
So, first off, your JSON isn't valid. You can copy and paste it into a site like
http://jsonlint.com/
to help with that. (You have a trailing comma at the end. The other issue is, you have two entries for "username" on each entry (a number and then a string), so the first is going to get overwritten by the second. Thirdly, if you are looking for an email address, your JSON doesn't contain any email addresses. I'll assume you meant username.
Below is some code that iterates through your JSON and prints out the value of username.
<?php
$json = '[{"username":"23441","username":"vanthien","phone":"0293029230"},{"username":"23442","username":"hoangtung","phone":"0599799930"},
{"username":"23443","username":"thanhtung","phone":"069929230"},
{"username":"23444","username":"redlight","phone":"0293299230"}]';
$arr = json_decode($json);
//echo print_r($arr, true);
foreach ($arr as $value) {
echo $value->username . PHP_EOL;
}
?>
Once you validate your json and have an email address to extract you can use json_decode
$parsed = json_decode($json, true);
Using the true flag will create an associative array
$email= $parsed['email'];

Categories