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
(
)
)
)