0

I want to remove extra array from my json. So need a help.

This is how my current json appear:

The current json

The below image shows, what i need (Without array):

The JSON I want

This is my php code

<?php 

define('HOST','localhost');
define('USER','appzcvfy_admin');
define('PASS','adminroot1');
define('DB','appzcvfy_sinala_short_stories');


$conn = mysqli_connect(HOST,USER,PASS,DB);

$sql1 = "select * from table_3";

$result1=mysqli_query($conn,$sql1);

$MainArray = array();
$Facilities = array();
$OpeningHours = array();

while ($row1=mysqli_fetch_assoc($result1)) {

    $Facilities['t1_id'] = $row1['id'];
    $Facilities['t1_name'] = $row1['name'];
    $Facilities['t1_link'] = $row1['link'];

    $Facilities['OpeningHours'] = array();

    $sql2 = "SELECT * FROM table_4 WHERE (id) = ".$row1['id']."";
    $result2=mysqli_query($conn,$sql2);

    while ($row2=mysqli_fetch_assoc($result2)) {

        $OpeningHours['t2_id'] = $row2['id'];
        $OpeningHours['t2_name'] = $row2['name'];
        $OpeningHours['t2_link'] = $row2['link'];
        $OpeningHours['t2_url'] = $row2['url'];

        array_push($Facilities['OpeningHours'],$OpeningHours);
    }    

    array_push($MainArray,$Facilities);
}

$jsonData = json_encode(array('server_respnose' => $MainArray), JSON_PRETTY_PRINT);

echo $jsonData;

?>

Can anyone rearrange my php code as my wanting. Thank you

2
  • 1
    why not join the two tables rather than have nested queries like this? Commented Dec 23, 2019 at 13:49
  • Please PASTE that JSON, we hate screenshots, we cant do anything with a screenshot Commented Dec 23, 2019 at 14:48

3 Answers 3

1

I've just seen before posting that I may have been pipped to the post with the table joins but here goes anyway ~ it might help simplify matters.

<?php

    define('HOST','localhost');
    define('USER','appzcvfy_admin');
    define('PASS','adminroot1');
    define('DB','appzcvfy_sinala_short_stories');


    $conn = mysqli_connect( HOST, USER, PASS, DB );
    $data=array();

    $sql='select 
            t3.`id` as `id1`, 
            t3.`name` as `name1`, 
            t3.`link` as `link11, 
            t4.`id` as `id2`, 
            t4.`name` as `name2`,
            t4.`link` as `link2`,
            t4.`url` 
        from table_3 t3 
        join table_4 t4 on t3.id=t4.id';

    $res=$conn->query( $sql );
    if( $res ){
        while( $rs=$res->fetch_object() ){
            $data[]=array(
                't1_id'     =>  $rs->id1,
                't1_name'   =>  $rs->name1,
                't1_link'   =>  $rs->link1,
                'OpeningHours'  =>  array(
                    't2_id'     =>  $rs->id2,
                    't2_name'   =>  $rs->name2,
                    't2_link'   =>  $rs->link2,
                    't2_url'    =>  $rs->url,
                )
            );      
        }
        $payload=array('server response'=>$data);
        $json=json_encode( $payload );
        echo $json;
    }
?>
Sign up to request clarification or add additional context in comments.

Comments

1

Assuming that you only have one row you don't need a loop, rather than creating a temporary array and then adding it, just set the values directly into the element...

$row2=mysqli_fetch_assoc($result2);
$Facilities['OpeningHours']['t2_id'] = $row2['id'];
$Facilities['OpeningHours']['t2_name'] = $row2['name'];
$Facilities['OpeningHours']['t2_link'] = $row2['link'];
$Facilities['OpeningHours']['t2_url'] = $row2['url'];

As suggested by RamRaider, you could reduce this to 1 query

select t3.id as t1_id, t3.name as t1_name, 
        t3.link as t1_link, t3.url as t1_url, 
        t4.id as t2_id, t4.name as t2_name, 
        t4.link as t2_link, t4.url as t2_url
    from table_3 t3
    join table_4 t4 on t4.id = t3.id

and just fetch the details to output from this instead - read his answer for a fuller explanation of this.

Comments

0

You declare it as an array(), so delete that bit:

$Facilities['OpeningHours'] = array(); // delete this

And replace this:

array_push($Facilities['OpeningHours'],$OpeningHours);

with this:

$Facilities['OpeningHours'] = $OpeningHours;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.