I am new to JSON and I am wondering how i could format my JSON file so I will be able to render it in a barchart.
I've got the following PHP code:
<?php
$search_value=$_POST["search"];
$mysqli = new mysqli('localhost','root','password','mydb');
$myArray = array();
if ($result = $mysqli->query("SELECT * FROM transcriptome WHERE genename LIKE '%$search_value%'")) {
while($row = $result->fetch_array(MYSQL_ASSOC)) {
$myArray = $row;
}
file_put_contents('jsonoutput.json', json_encode($myArray));
echo json_encode($myArray);
}
$result->close();
$mysqli->close();
?>
My actual output:
(given a gene (xkr4) as input)
{"genename":"xkr4","TA11MEAN":"974.25","TA11STD":"99.0085223605","TA21MEAN":"710.75","TA21STD":"115.79831605","TA22MEAN":"736.5","TA22STD":"115.79831605","TA23MEAN":"903.75","TA23STD":"107.283211641","TB11MEAN":"799.25","TB11STD":"97.2660655111","TB21MEAN":"658","TB21STD":"91.7959694104","TB22MEAN":"592.75","TB22STD":"70.9379129944","TB23MEAN":"864","TB23STD":"92.7280971443"}
How I'd like to get my output:
{"genename":"xkr4",{"TA11MEAN":"974.25"},{"TA11STD":"99.0085223605"},{"TA21MEAN":"710.75"},{"TA21STD":"115.79831605"},{"TA22MEAN":"736.5"},{"TA22STD":"115.79831605"},{"TA23MEAN":"903.75"},{"TA23STD":"107.283211641"},{"TB11MEAN":"799.25"},{"TB11STD":"97.2660655111"},{"TB21MEAN":"658"},{"TB21STD":"91.7959694104"},{"TB22MEAN":"592.75"},{"TB22STD":"70.9379129944"},{"TB23MEAN":"864"},{"TB23STD":"92.7280971443"}}
If someone could give me direction on this (or solve it) That would be great!
Thanks in advance :)
I would suggest you to first check how are you getting back your data in array.
$myArray[] should have the data populated as shown below. Then use JSON_PRETTY_PRINT.
/*USED TO SHOW FULL ARRAY SIZE*/
ini_set('xdebug.var_display_max_depth', -1);
ini_set('xdebug.var_display_max_children', -1);
ini_set('xdebug.var_display_max_data', -1);
$myArray=array("genename"=>array(array("xkr4"=>array(
array("TA11MEAN"=>"974.25","TA11STD"=>"99.0085223605"),
array("TA21MEAN"=>"710.75","TA21STD"=>"115.79831605"),
array("TA22MEAN"=>"736.5","TA22STD"=>"115.79831605"),
array("TA23MEAN"=>"903.75","TA23STD"=>"107.283211641"),
array("TB11MEAN"=>"799.25","TB11STD"=>"97.2660655111"),
array("TB21MEAN"=>"658","TB21STD"=>"91.7959694104"),
array("TB22MEAN"=>"592.75","TB22STD"=>"70.9379129944"),
array("TB23MEAN"=>"864","TB23STD"=>"92.7280971443"),
))));
$jsonData=json_encode($myArray,JSON_PRETTY_PRINT);
var_dump($jsonData);
$json_from_database='[{"genename":"xkr4","TA11MEAN":"974.25","TA11STD":"99.0085223605","TA21MEAN":"710.75","TA21STD":"115.79831605","TA22MEAN":"736.5","TA22STD":"115.79831605","TA23MEAN":"903.75","TA23STD":"107.283211641","TB11MEAN":"799.25","TB11STD":"97.2660655111","TB21MEAN":"658","TB21STD":"91.7959694104","TB22MEAN":"592.75","TB22STD":"70.9379129944","TB23MEAN":"864","TB23STD":"92.7280971443"}]';
//print decode array from databse
$decoded=json_decode($json_from_database);
var_dump($decoded);
foreach ($decoded[0] as $key => $value) {
echo "\n ";
print $key;
print " " .$decoded[0]->$key;
}
array(1) {
[0]=>
object(stdClass)#1 (17) {
["genename"]=>
string(4) "xkr4"
["TA11MEAN"]=>
string(6) "974.25"
["TA11STD"]=>
string(13) "99.0085223605"
["TA21MEAN"]=>
string(6) "710.75"
["TA21STD"]=>
string(12) "115.79831605"
["TA22MEAN"]=>
string(5) "736.5"
["TA22STD"]=>
string(12) "115.79831605"
["TA23MEAN"]=>
string(6) "903.75"
["TA23STD"]=>
string(13) "107.283211641"
["TB11MEAN"]=>
string(6) "799.25"
["TB11STD"]=>
string(13) "97.2660655111"
["TB21MEAN"]=>
string(3) "658"
["TB21STD"]=>
string(13) "91.7959694104"
["TB22MEAN"]=>
string(6) "592.75"
["TB22STD"]=>
string(13) "70.9379129944"
["TB23MEAN"]=>
string(3) "864"
["TB23STD"]=>
string(13) "92.7280971443"
}
}
genename xkr4
TA11MEAN 974.25
TA11STD 99.0085223605
TA21MEAN 710.75
TA21STD 115.79831605
TA22MEAN 736.5
TA22STD 115.79831605
TA23MEAN 903.75
TA23STD 107.283211641
TB11MEAN 799.25
TB11STD 97.2660655111
TB21MEAN 658
TB21STD 91.7959694104
TB22MEAN 592.75
TB22STD 70.9379129944
TB23MEAN 864
TB23STD 92.7280971443
This is how your data looks like from database it is a array of rows row is a object
I solved it:
<?php
$search_value=$_POST["search"];
$mysqli = new mysqli('localhost','root','password','mydb');
$myArray = array();
if ($result = $mysqli->query("SELECT * FROM transcriptome WHERE genename LIKE '%$search_value%'")) {
while($row = $result->fetch_array(MYSQL_ASSOC)) {
$myArray = $row;
}
//file_put_contents('jsonoutput.json', json_encode($myArray));
$json = json_encode($myArray);
$array = json_decode($json, true);
$new_array = array();
foreach( $array as $key => $value ){
$newarray[] = array($key=>$value);
}
echo json_encode($newarray);
file_put_contents('jsonoutput.json', json_encode($newarray));
}
$result->close();
$mysqli->close();
?>
I'm saving data inside arrays with php's json_encode() function and then encoding them into json strings in order to save them into a single database field.
This is what I've been doing:
$fechaMensaje = $_POST['fechaMensaje'];
$mensaje = $_POST['mensaje'];
$estadoMensaje = 'abierto';
$data['fecha'][] = $fechaMensaje;
$data['autor'][] = $userEmail;
$data['mensaje'][] = $mensaje;
$datosMensaje = json_encode($data);
This does work, and it does creates a string like this:
{
"fecha":["29-09-2016 11:12:51 AM"],
"autor":["demo#example.com"],
"mensaje":["lorem ipsum"]
}
This is the array I've got when decoding the string:
{
["fecha"]=> array(1) {
[0]=> string(22) "29-09-2016 11:12:51 AM"
}
["autor"]=> array(1) {
[0]=> string(23) "demo#example.com"
}
["mensaje"]=> array(1) {
[0]=> string(11) "lorem ipsum"
}
}
Now, my question is, how may I change the way I'm generating the array in the first place, to get this output instead? (having the three items in the same array, so when I do add more elements it's going to be more organized).
{
["0"]=> array(3) {
['fecha']=> string(22) "29-09-2016 11:12:51 AM"
['autor']=> string(23) "demo#example.com"
['mensaje']=> string(11) "lorem ipsum"
}
[1]=> array(3) {
...
...
...
}
}
You can try this code:
$obj['fecha'] = $fechaMensaje;
$obj['autor'] = $userEmail;
$obj['mensaje'] = $mensaje;
//insert obj to data array
$data[] = $obj;
// encoding to json
$json = json_encode($data);
You can define another array, in the below case $some_var, to contain each array of data. Also remove [] on the end when assigning the values for $data.
$fechaMensaje = $_POST['fechaMensaje'];
$mensaje = $_POST['mensaje'];
$estadoMensaje = 'abierto';
$data['fecha'] = $fechaMensaje;
$data['autor'] = $userEmail;
$data['mensaje'] = $mensaje;
$some_var[0] = $data;
$datosMensaje = json_encode($some_var);
I am trying to retrieve data from this array in php.
array(2) {
["getWysiwyg"]=>
string(37) "[{"basicsDescription":"<p><br></p>"}]"
["getGoal"]=>
string(27) "[{"iconURL":"","title":""}]"
}
I tried Input::get('getWysiwyg') it returns [{"basicsDescription":"<p><br></p>"}]
Now how could i get the value i.e <p><br></p>
As I see your array items are json encoded ..
Try to decode them as this:
foreach($array as $key=>$value){
$decodedValue = json_decode($value, true);
print_r($decodedValue);
}
You have to use json_decode(), because the string [{"basicsDescription":"<p><br></p>"}]represents an array with an object in JSON.
$string = '[{"basicsDescription":"<p><br></p>"}]';
$objectArray = json_decode( $string );
$objectArray now looks like:
array(1) {
[0]=>
object(stdClass)#1 (1) {
["basicsDescription"]=>
string(11) "<p><br></p>"
}
}
To get the value of basicsDescription you need to access the array in this case with the index 0, then you have the object:
$object = $objectArray[0];
Once you've got the object you can access it's attributes with the object operator ->:
$object->basicsDescription;// content: <p><br></p>
Short form of this:
$string = '[{"basicsDescription":"<p><br></p>"}]';// in your case Input::get('getWysiwyg')
$objectArray = json_decode( $string );
$objectArray[0]->basicsDescription;
If it's possible, that there are more than one item in it, you should go for foreach
If all items of your array representing JSON strings you can use array_map():
$array = array(
"getWysiwyg" => '[{"basicsDescription":"<p><br></p>"}]',
"getGoal" => '[{"iconURL":"","title":""}]'
);
$array = array_map( 'json_decode' , $array );
echo "<pre>";
var_dump( $array );
This will output:
array(2) {
["getWysiwyg"]=>
array(1) {
[0]=>
object(stdClass)#1 (1) {
["basicsDescription"]=>
string(11) "<p><br></p>"
}
}
["getGoal"]=>
array(1) {
[0]=>
object(stdClass)#2 (2) {
["iconURL"]=>
string(0) ""
["title"]=>
string(0) ""
}
}
}
Decode and print as follows
$object = json_decode(Input::get('getWysiwyg'));
print $object[0]->basicsDescription;
or directly with the help of array dereferencing
print json_decode(Input::get('getWysiwyg'))[0]->basicsDescription;
will output
<p><br></p>
Here's the relevant part of my PHP:
$response2 = curl_exec( $ch2 );
$stuff2 = json_decode($response2, true);
$user_id = $stuff2['identities[0].user_id'];
The $response looks good, as does $stuff2. What I'm trying to get is the value for user_id and store as a variable I can use in PHP ($user_id). If I var_dump $stuff2, I get something like this:
array(2) {
["nickname"]=>
string(3) "jmr"
["identities"]=>
array(1) {
[0]=>
array(2) {
["user_id"]=>
string(24) "5458fec4aa0395931002fe71"
["connection"]=>
string(32) "Username-Password-Authentication"
}
}
}
If I do this:
$response2 = curl_exec( $ch2 );
$stuff2 = json_decode($response2, true);
$user_id = $stuff2['nickname'];
then I get "jmr" no problem, but I can't figure out how to get that "user_id" buried in that second array within the array.
Do a print_r($stuff2); and you will see the array is just an ordinary multi-dimensional array. I'm not sure why you have that convoluted key, but you access it just like an ordinary multi-dimensional array:
$user_id = $stuff2['identities'][0]['user_id'];
I've been trying for hours now to get the title field from the json code. Below is my php code
$search = $_GET['search'];
$new = str_replace(' ', '+', $search);
$url = "http://api.themoviedb.org/3/search/movie?api_key=###&query=".$new;
$json = file_get_contents($url);
$json_data = json_decode($json, true);
$title = $json_data->title;
echo $title;
this is the var dump of the json
array(4) { ["page"]=> int(1) ["results"]=> array(1) { [0]=> array(10) { ["adult"]=> bool(false) ["backdrop_path"]=> string(32) "/4uJZvmEMX6Z6ag3bzym5exLY9wI.jpg" ["id"]=> int(65) ["original_title"]=> string(6) "8 Mile" ["release_date"]=> string(10) "2002-11-08" ["poster_path"]=> string(32) "/dXzTrKwpbLpCqn8O70FUUhNbYQT.jpg" ["popularity"]=> float(3.792332418578) ["title"]=> string(6) "8 Mile" ["vote_average"]=> float(6.2) ["vote_count"]=> int(185) } } ["total_pages"]=> int(1) ["total_results"]=> int(1) }
the error i keep getting is Notice: Trying to get property of non-object
any help would be greatly appreciated.
$json_data = json_decode($json, true);
will return an array not object
so you need to use as
$json_data["title"];
NOTE : Your json decoded array is nested so you may need to use as in your case.
$json_data["results"][0]["title"];
Or better loop through and get the desired data.
I just started using PHP and I THINK this might help you... but i'n not sure
foreach($json_data as $key => $value){
if($key == "results"){
$json_data2 = $value;
}
}
foreach($json_data2 as $key => $value){
if($key == "original_title"){
$title = $value;
}
}
echo $title;