1

I am running some SQL query to get the data and then I am adding them into an array. I am trying to achieve the data in nested array. As school table is returning multiple data for users table.

Here is the code.

$res=array();

$data = $mysqli->prepare("SELECT name, rollno, pic from `users` as a and a.uid = ?"); 
$data ->bind_param("i", $uid);
$data->execute();
$data->store_result();
$data->bind_result($name, $rollno, $pic);
                        
if($data->num_rows != 0){
    while ($data->fetch()){
            $name = $name;
            $rollno = $rollno;
            $pic = $pic;    
            
        $res['content'][] = array(
                    "name" => $name,
                    "rollno" => $rollno,
                    "pic" => $pic
        );      
    

$sdata = $mysqli->prepare("SELECT a.uid, a.class, a.subject, a.board from `school` as a and a.uid = ?"); 
$sdata ->bind_param("i", $uid);
$sdata->execute();
$sdata->store_result();
$sdata->bind_result( $uid, $class, $subject, $board);
                        
        if($sdata->num_rows != 0){
            while ($sdata->fetch()){
                    $uid = $uid;
                    $class = $class;
                    $subject = $subject;
                    $board = $board;    
                    
                $res['content']['pricing'][] = array(
                            "uid" => $uid,
                            "class" => $class,
                            "subject" => $subject,
                            "board" => $board
                );      
            }
            }
        }
    }

Above method is giving me output like this.

Array
(
    [content] => Array
        (
            [0] => Array
                (
                    [name] => Test User1
                    [rollno] => 9
                    [pic] => 9_1599452969.jpg
                   
                )

            [1] => Array
                (
                    [name] => Test User2
                    [rollno] => 8
                    [pic] => 8_1599452969.jpg
                    
                )

            [pricing] => Array
                (
                    [0] => Array
                        (
                            [uid] => 8
                            [class] => 2
                            [subject] => Math
                            [board] => CBSE
                        )

                    [1] => Array
                        (
                            [uid] => 8
                            [class] => 2
                            [subject] => Science
                            [board] => CBSE
                        )

                    [2] => Array
                        (
                            [uid] => 9
                            [class] => 2
                            [subject] => Science
                            [board] => CBSE
                        )

                    [3] => Array
                        (
                            [uid] =>9
                            [class] => 2
                            [subject] => English
                            [board] => CBSE
                        )

                    [4] => Array
                        (
                            [uid] => 9
                            [class] => 2
                            [subject] => Math
                            [board] => CBSE
                        )

                )

        )

)

As you can see pricing array data is coming after Content data. I want it to be in same line in nested way.

  Array
(
    [content] => Array
        (
            [0] => Array
                (
                    [name] => Test User1
                    [rollno] => 9
                    [pic] => 9_1599452969.jpg
                    [Pricing][0] => Array
                        (
                            [uid] => 9
                            [class] => 2
                            [subject] => Science
                            [board] => CBSE
                        )

                    [1] => Array
                        (
                            [uid] =>9
                            [class] => 2
                            [subject] => English
                            [board] => CBSE
                        )

                    [2] => Array
                        (
                            [uid] => 9
                            [class] => 2
                            [subject] => Math
                            [board] => CBSE
                        ) 
                   
                )

            [1] => Array
                (
                    [name] => Test User2
                    [rollno] => 8
                    [pic] => 8_1599452969.jpg
                    [pricing][0] => Array
                        (
                            [uid] => 8
                            [class] => 2
                            [subject] => Math
                            [board] => CBSE
                        )

                    [1] => Array
                        (
                            [uid] => 8
                            [class] => 2
                            [subject] => Science
                            [board] => CBSE
                        )
                )

            [pricing] => Array
                (
                    

                    

                )

        )

)

1 Answer 1

1

The simplest approach would be to create temporary array, feed it with all data and then add to $res. So instead of

        $res['content'][] = array(
                    "name" => $name,
                    "rollno" => $rollno,
                    "pic" => $pic
        );      

do

        $tmp = array(
                    "name" => $name,
                    "rollno" => $rollno,
                    "pic" => $pic
        );      

Then add pricing to it - instead of

                $res['content']['pricing'][] = array(
                            "uid" => $uid,
                            "class" => $class,
                            "subject" => $subject,
                            "board" => $board
                );      

add to temporary array

                $tmp['pricing'][] = array(
                            "uid" => $uid,
                            "class" => $class,
                            "subject" => $subject,
                            "board" => $board
                );     

And finally, before doing another iteration of main loop add it to $res

$res['content'][] = $tmp;
Sign up to request clarification or add additional context in comments.

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.