Jump to content

PHP output in JSON


RandomDNA

Recommended Posts

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 :)
Edited by RandomDNA
Link to comment
Share on other sites

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}]
Edited by Sepodati
  • Like 1
Link to comment
Share on other sites

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

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();
?>
Edited by RandomDNA
Link to comment
Share on other sites

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();
?>
  • Like 1
Link to comment
Share on other sites

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. 

Edited by Gandalf64
Link to comment
Share on other sites

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.