RandomDNA Posted September 20, 2017 Share Posted September 20, 2017 Hi, I'm new to PHP and JSON and I'm wondering how i could change my output from: [{"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"}] to: [{"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"}] My script: <?php $search_value=$_POST["search"]; $mysqli = new mysqli('localhost','admin','pass','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(); ?> Thanks in advance Link to comment Share on other sites More sharing options...
requinix Posted September 20, 2017 Share Posted September 20, 2017 Use a foreach on $row to get each column and value, and put the two together into a new array (as key and value) which you add to $myArray. Link to comment Share on other sites More sharing options...
Sepodati Posted September 20, 2017 Share Posted September 20, 2017 Is that one row of the results from the query? And you want each column to be an object? Is that how I'm reading this? Any reason why? Oh, nevermind... as Req said. <?php $myarray = array('one'=>1,'two'=>2,'three'=>3,'four'=>4); $newarray = array(); foreach($myarray as $key=>$value) { $newarray[] = array($key=>$value); } echo json_encode($newarray); ?> Output: [{"one":1},{"two":2},{"three":3},{"four":4}] Link to comment Share on other sites More sharing options...
RandomDNA Posted September 20, 2017 Author Share Posted September 20, 2017 Thanks requinix and Sepodati for your replies I'm still figuring out how the script should be? Is that one row of the results from the query? And you want each column to be an object? Is that how I'm reading this? Any reason why?Oh, nevermind... as Req said. <?php $myarray = array('one'=>1,'two'=>2,'three'=>3,'four'=>4); $newarray = array(); foreach($myarray as $key=>$value) { $newarray[] = array($key=>$value); } echo json_encode($newarray); ?> Output: [{"one":1},{"two":2},{"three":3},{"four":4}] Should i "load" the query in the $newarray? I'm sorry for my low understanding... Link to comment Share on other sites More sharing options...
Sepodati Posted September 20, 2017 Share Posted September 20, 2017 Maybe answer the "why" you think you need to do this, then? Link to comment Share on other sites More sharing options...
RandomDNA Posted September 20, 2017 Author Share Posted September 20, 2017 This format is required for a barchart Link to comment Share on other sites More sharing options...
RandomDNA Posted September 20, 2017 Author Share Posted September 20, 2017 I got it <?php $search_value=$_POST["search"]; $mysqli = new mysqli('localhost','root','usbw','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); } $result->close(); $mysqli->close(); ?> Link to comment Share on other sites More sharing options...
Sepodati Posted September 20, 2017 Share Posted September 20, 2017 That encode/decode in the middle there seems wasteful. Wouldn't this do the same thing? <?php $search_value=$_POST["search"]; $mysqli = new mysqli('localhost','root','usbw','mydb'); $myArray = array(); if ($result = $mysqli->query("SELECT * FROM transcriptome WHERE genename LIKE '%$search_value%'")) { while($row = $result->fetch_array(MYSQL_ASSOC)) { foreach($row as $key=>$value) { $myArray[] = array($key=>$value); } } //file_put_contents('jsonoutput.json', json_encode($myArray)); echo json_encode($myArray); } $result->close(); $mysqli->close(); ?> Link to comment Share on other sites More sharing options...
Gandalf64 Posted September 21, 2017 Share Posted September 21, 2017 Someone awhile back told me that I should do something like the following (Just an example): <?php require_once '../private/initialize.php'; use Library\Display\Display; $status = FALSE; $display = new Display(); if (is_logged_in()) { $status = TRUE; } /* Makes it so we don't have to decode the json coming from Javascript */ header('Content-type: application/json'); /* Start of your php routine(s) */ $submit = filter_input(INPUT_POST, 'submit', FILTER_SANITIZE_FULL_SPECIAL_CHARS); if (isset($submit)) { $user_id = filter_input(INPUT_POST, 'user_id', FILTER_SANITIZE_FULL_SPECIAL_CHARS); $data = $display->readBlog("blog.php", $user_id); if ( (isset($_SESSION['user']) && $_SESSION['user']->id === (int)$user_id) || (isset($_SESSION['user']) && $_SESSION['user']->security_level === 'sysop') ) { $temp = true; } else { $temp = false; } array_unshift($data, $temp); output($data); } /* End of your php routine(s) */ /* * If you know you have a control error, for example an end-of-file then simply output it to the errorOutput() function */ function errorOutput($output, $code = 500) { http_response_code($code); echo json_encode($output); } /* * If everything validates OK then send success message to Ajax / JavaScript */ function output($output) { http_response_code(200); echo json_encode($output); } And so far it was work out like a charm for me, it forces you to be structure and it's also pretty easy to debug doing it this way. Link to comment Share on other sites More sharing options...
RandomDNA Posted September 21, 2017 Author Share Posted September 21, 2017 That encode/decode in the middle there seems wasteful. Wouldn't this do the same thing? <?php $search_value=$_POST["search"]; $mysqli = new mysqli('localhost','root','usbw','mydb'); $myArray = array(); if ($result = $mysqli->query("SELECT * FROM transcriptome WHERE genename LIKE '%$search_value%'")) { while($row = $result->fetch_array(MYSQL_ASSOC)) { foreach($row as $key=>$value) { $myArray[] = array($key=>$value); } } //file_put_contents('jsonoutput.json', json_encode($myArray)); echo json_encode($myArray); } $result->close(); $mysqli->close(); ?> Gives an error: Fatal error: Call to a member function close() on a non-object Link to comment Share on other sites More sharing options...
Sepodati Posted September 21, 2017 Share Posted September 21, 2017 Gives an error: Fatal error: Call to a member function close() on a non-object That has nothing to do with building the array in the fetch loop. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.