json响应查询

5rgfhyps  于 2021-06-25  发布在  Mysql
关注(0)|答案(4)|浏览(312)

我现在正在从下表中的数据获取一个json响应

通过使用以下查询:

$sql = "select * from subject where subject_name = 'maths'";

$result =  mysqli_query($conn,$sql);

    while($row = mysqli_fetch_array($result))
    {
    $data = new stdClass();
    $subject_name = $row['subject_name'];
    $unit = $row['unit'];
    $unit_name = $row['unit_name'];

    $data->subject_name=$subject_name;

    $emparray[] = array('unit' => $unit,
                        'unit_name' => $unit_name,
                        );

     $data->units=$emparray;
    }

echo json_encode(array($data));

我可以得到如下json响应:

现在我想要的是不使用where子句的所有科目(where subject\u name='数学')
我需要的json o/p如下:

0vvn1miw

0vvn1miw1#

您可以通过以下代码将结果按主题名称分组到php数组中。

$sql    = "select * from users";

$sql    = "select subject_name,unit,unit_name from users";

PHP代码

$result =  mysqli_query($conn,$sql);
$data   = array();

function search($subject){
  global $data;
  foreach ($data as $key => $value) {
    if (isset($value['subject_name']) && $value['subject_name']==$subject) {
        return $key;
      }
  }
 return false;
}

while($row = mysqli_fetch_assoc($result)){
  $res = search($row['subject_name']);
  if ($res===false) {
    array_push($data, array(
            'subject_name' =>$row['subject_name'],
            'units'=>array($row)
        )
    );
  }else{
    array_push($data[$res]['units'], $row);
  }
}

echo json_encode($data);

现在您可以从上面的代码中获得json格式。

am46iovg

am46iovg2#

你可以试试这个代码

$sql = "select * from subject;

    $result =  mysqli_query($conn,$sql);

        while($query_row= mysqli_fetch_array($result))
        {

    if($query_row['subject_name'] == 'English'){
                        $timeslot = array(
                            "unit"      => $query_row['unit'],
                            "unit_name" => $query_row['unit_name']
                        );

                        $subjects   = addToArray($subjects, $timeslot['subject_name']);

                    } else if($query_row['subject_name'] == 'Maths'){
                        $timeslot = array(
                        "unit"      => $query_row['unit'],
                            "unit_name" => $query_row['unit_name']
                        );

                        $subjects   = addToArray($subjects, $timeslot['subject_name']);

                    } else if($query_row['subject_name'] == 'Science'){
                        $timeslot = array(
                            "unit"      => $query_row['unit'],
                            "unit_name" => $query_row['unit_name']
                        );
        $subjects   = addToArray($subjects, $timeslot['subject_name']);
                    }

    }
3df52oht

3df52oht3#

尝试:

$sql = " select subject_name, group_concat(unit) unit, group_concat(unit_name) unit_name
          from subject group by subject_name ";

$result = mysqli_query($conn, $sql);

$data = [];
while($row = mysqli_fetch_array($result))
{
    $subject_name = $row['subject_name'];
    $units = explode( ',', $row['unit'] );
    $unit_names = explode( ',', $row['unit_name'] );

    $subject = [];
    $subject['subject_name'] = $subject_name;

    $emparray = [];
    for ($i=0; $i < count($units); $i++) {
        $emparray[] = array('unit' => $units[$i], 'unit_name' => $unit_names[$i]);
    }

    $subject['units'] = $emparray;
    $data[] = $subject;
}

echo json_encode($data);
zujrkrfu

zujrkrfu4#

我的代码可能不完全正确,但仍然可以帮助您

$outer_sql = "SELECT DISTINCT subject_name * from subject;
  $outer_result =  mysqli_query($conn,$outer_sql);
  while($outer_query_row= mysqli_fetch_array($outer_result))
  {
 $sql = "select * from subject where subject_name = '".$outer_query_row['subject_name']."'";

 $result =  mysqli_query($conn,$sql);
  while($row = mysqli_fetch_array($result))
{
$data = new stdClass();
$subject_name = $row['subject_name'];
$unit = $row['unit'];
$unit_name = $row['unit_name'];

$data->subject_name=$subject_name;

$emparray[] = array('unit' => $unit,
                    'unit_name' => $unit_name,
                    );

 $data->units=$emparray;
}

  }
echo json_encode(array($data));

相关问题