如何使用PHP按键值对JSON数组排序

时间:2018-07-24 12:20:48

标签: php mysql arrays

我需要使用PHP和MySQL按照相同的键值以某种格式对json数组值进行排序。我在下面解释我的代码。

require_once('dbconn.php');
$con = dbConnect();
$group='BA_2017_ANT_ODDSEM';
$query='select egm.exam_group_name, ezm.zone_name, ecm.exam_centre_name,
               im.institute_name 
        from institute_to_centre_mapping icm 
            join institute_master im on icm.institute_code=im.institute_code 
            join exam_zone_master ezm on icm.zone_code= ezm.zone_code 
            join exam_centre_master ecm on icm.exam_centre_code= ecm.exam_centre_code 
            join exam_group_master egm on icm.exam_group_code=egm.exam_group_code 
        where icm.exam_group_code="'.$group.'" 
        and icm.record_status=1 
        order by ezm.zone_name,ecm.exam_centre_name, im.institute_name';

$rResult = mysqli_query($con,$query);
while ($row=mysqli_fetch_array($rResult)) {
    $output[]=$row;
}
echo json_encode($output);

上面的结果如下。

[{  "0":"BA 2017-18_ANT_ODD",
    "exam_group_name":"BA 2017-18_ANT_ODD",
    "1":"EASTZONE",
    "zone_name":"EASTZONE",
    "2":"Berhampur",
    "exam_centre_name":"Berhampur",
    "3":"BALIMELA COLLEGE OF SCIENCE & TECHNOLOGY",
    "institute_name":"BALIMELA COLLEGE OF SCIENCE & TECHNOLOGY"
},{
    "0":"BA 2017-18_ANT_ODD",
    "exam_group_name":"BA 2017-18_ANT_ODD",
    "1":"EASTZONE",
    "zone_name":"EASTZONE",
    "2":"Berhampur",
    "exam_centre_name":"Berhampur",
    "3":"BIJU PATNAIK WOMEN`S COLLEGE,DIGAPAHANDI",
    "institute_name":"BIJU PATNAIK WOMEN`S COLLEGE,DIGAPAHANDI"
},{
    "0":"BA 2017-18_ANT_ODD",
    "exam_group_name":"BA 2017-18_ANT_ODD",
    "1":"EASTZONE",
    "zone_name":"EASTZONE",
    "2":"Berhampur",
    "exam_centre_name":"Berhampur",
    "3":"C.A.C.,GANDHI NAGAR MAIN ROAD, BERHAMPUR",
    "institute_name":"C.A.C.,GANDHI NAGAR MAIN ROAD, BERHAMPUR"
},{
    "0":"BA 2017-18_ANT_ODD","exam_group_name":"BA 2017-18_ANT_ODD","1":"EASTZONE","zone_name":"EASTZONE","2":"Berhampur","exam_centre_name":"Berhampur","3":"DR.B.R.AMBEDKAR DEGREE COLLEGE, RAMAGIRI","institute_name":"DR.B.R.AMBEDKAR DEGREE COLLEGE, RAMAGIRI"},{"0":"BA 2017-18_ANT_ODD","exam_group_name":"BA 2017-18_ANT_ODD","1":"EASTZONE","zone_name":"EASTZONE","2":"Berhampur","exam_centre_name":"Berhampur","3":"G.S.SCIENCE DEGREE COLLEGE,HATIOTO,GANJM","institute_name":"G.S.SCIENCE DEGREE COLLEGE,HATIOTO,GANJM"},{"0":"BA 2017-18_ANT_ODD","exam_group_name":"BA 2017-18_ANT_ODD","1":"EASTZONE","zone_name":"EASTZONE","2":"Berhampur","exam_centre_name":"Berhampur","3":"SRI BELLESWAR MAHAVIDYALAYA, GONDALA","institute_name":"SRI BELLESWAR MAHAVIDYALAYA, GONDALA"},{"0":"BA 2017-18_ANT_ODD","exam_group_name":"BA 2017-18_ANT_ODD","1":"EASTZONE","zone_name":"EASTZONE","2":"Bhadrak","exam_centre_name":"Bhadrak","3":"AERONAUTICS COLLEGE, SUNABEDA","institute_name":"AERONAUTICS COLLEGE, SUNABEDA"},{"0":"BA 2017-18_ANT_ODD","exam_group_name":"BA 2017-18_ANT_ODD","1":"EASTZONE","zone_name":"EASTZONE","2":"Bhadrak","exam_centre_name":"Bhadrak","3":"B.S.AMBEDKAR DEGREE COLLEGE, KHAJURIPADA","institute_name":"B.S.AMBEDKAR DEGREE COLLEGE, KHAJURIPADA"},{"0":"BA 2017-18_ANT_ODD","exam_group_name":"BA 2017-18_ANT_ODD","1":"EASTZONE","zone_name":"EASTZONE","2":"Bhubaneswar","exam_centre_name":"Bhubaneswar","3":"ADIBASI COLLEGE, BALLIGUDA","institute_name":"ADIBASI COLLEGE, BALLIGUDA"},{"0":"BA 2017-18_ANT_ODD","exam_group_name":"BA 2017-18_ANT_ODD","1":"EASTZONE","zone_name":"EASTZONE","2":"Bhubaneswar","exam_centre_name":"Bhubaneswar","3":"ANCHALIK SCIENCE COLLEGE, BALLIPADAR","institute_name":"ANCHALIK SCIENCE COLLEGE, BALLIPADAR"},{"0":"BA 2017-18_ANT_ODD","exam_group_name":"BA 2017-18_ANT_ODD","1":"WESTZONE","zone_name":"WESTZONE","2":"Centre 34","exam_centre_name":"Centre 34","3":"BHAIRAB COLLEGE, DABUGAON","institute_name":"BHAIRAB COLLEGE, DABUGAON"},{"0":"BA 2017-18_ANT_ODD","exam_group_name":"BA 2017-18_ANT_ODD","1":"WESTZONE","zone_name":"WESTZONE","2":"Centre 34","exam_centre_name":"Centre 34","3":"BHAIRAB DEGREE MAHAVIDYALAYA, BORIGUMA","institute_name":"BHAIRAB DEGREE MAHAVIDYALAYA, BORIGUMA"},{"0":"BA 2017-18_ANT_ODD","exam_group_name":"BA 2017-18_ANT_ODD","1":"WESTZONE","zone_name":"WESTZONE","2":"Kanasa","exam_centre_name":"Kanasa","3":"KARUNASANTI RES. COLLEGE, GOLANTHARA,BAM","institute_name":"KARUNASANTI RES. COLLEGE, GOLANTHARA,BAM"},{"0":"BA 2017-18_ANT_ODD","exam_group_name":"BA 2017-18_ANT_ODD","1":"ZONE 1","zone_name":"ZONE 1","2":"Angul","exam_centre_name":"Angul","3":"ASKA SCIENCE COLLEGE, ASKA","institute_name":"ASKA SCIENCE COLLEGE, ASKA"},{"0":"BA 2017-18_ANT_ODD","exam_group_name":"BA 2017-18_ANT_ODD","1":"ZONE 1","zone_name":"ZONE 1","2":"Angul","exam_centre_name":"Angul","3":"MAA MARKAMA COLLEGE, BISSAMCUTTACK","institute_name":"MAA MARKAMA COLLEGE, BISSAMCUTTACK"}]

在这里,我需要根据重复的键值格式化这些数组,并且预期结果应如下所示。

[  
   {  
      "zone":"EASTZONE",
      "centerData":[  
         {  
            "center":"Berhampur",
            "subdata":[  
               {  
                  "Institute":"BALIMELA COLLEGE OF SCIENCE & TECHNOLOGY"
               },
               {  
                  "Institute":"BIJU PATNAIK WOMEN`S COLLEGE,DIGAPAHANDI"
               },
               {  
                  "Institute":"C.A.C.,GANDHI NAGAR MAIN ROAD, BERHAMPUR"
               }
            ],
            "cntspan":3
         },
         {  
            "center":"Bhadrak",
            "subdata":[  
               {  
                  "Institute":"AERONAUTICS COLLEGE, SUNABEDA"
               },
               {  
                  "Institute":"B.S.AMBEDKAR DEGREE COLLEGE, KHAJURIPADA"
               }
            ],
            "cntspan":2
         }
      ],
      "zonespan":5
   },
   {  
      "zone":"WESTZONE",
      "centerData":[  
         {  
            "center":"Centre 34",
            "subdata":[  
               {  
                  "Institute":"BHAIRAB COLLEGE, DABUGAON"
               },
               {  
                  "Institute":"BHAIRAB DEGREE MAHAVIDYALAYA, BORIGUMA"
               }
            ],
            "cntspan":2
         },
         {  
            "center":"Kanasa",
            "subdata":[  
               {  
                  "Institute":"KARUNASANTI RES. COLLEGE, GOLANTHARA,BAM"
               }
            ],
            "cntspan":1
         }
      ],
      "zonespan":3
   },
   {  
      "zone":"ZONE 1",
      "centerData":[  
         {  
            "center":"Angul",
            "subdata":[  
               {  
                  "Institute":"ASKA SCIENCE COLLEGE, ASKA"
               }
            ],
            "cntspan":1
         },
         {  
            "center":"Balasore",
            "subdata":[  
               {  
                  "Institute":"Institute10"
               }
            ],
            "cntspan":1
         }
      ],
      "zonespan":2
   }
]

以上是我期望的数组格式。这里的要求是数组将按照zone_name and exam_centre_name进行排序。中心计数和区域计数也将通过键cntspan and zonespan存在。我在下面解释我现有的代码。

$resultArr=array();
foreach ($output as $k => $value) {
    $zonecnt=0;
    if (count($resultArr)==0) {
        $resultArr[]=array("zone"=>$value['zone_name'],"centerData"=>array(array("center"=>$value['exam_centre_name'],"subdata"=>array(array("Institute"=>$value['institute_name'])))),'zonespan'=>$zonecnt+1);
    }else{
        foreach ($resultArr as $l => $v) {
            if ($resultArr[$l]['zone']==$value['zone_name']) {
                foreach ($v['centerData'] as $m => $val) {
                    if ($val['center']==$value['exam_centre_name']) {
                        $val['subdata'][]=array("Institute"=>$value['institute_name']);
                    }else{
                        $v['centerData'][]=array(array("center"=>$value['exam_centre_name'],"subdata"=>array(array("Institute"=>$value['institute_name']))));
                        break;
                    }
                }
            }else{
                $resultArr[]=array("zone"=>$value['zone_name'],"centerData"=>array(array("center"=>$value['exam_centre_name'],"subdata"=>array(array("Institute"=>$value['institute_name'])))),'zonespan'=>$zonecnt+1);
            }
        }

    }
}
echo json_encode($resultArr);exit;

在这里我无法获得适当的数组格式。我需要根据这些键值对数据库值进行排序。

1 个答案:

答案 0 :(得分:0)

通过匹配key =>值对,您可以尝试遍历json并按照新的数组格式重新组织数据。

这是我的意思的不完整示例

    $centers = array();    
    $current_zone = '';    

    foreach($jsondata as $key=>$value){            
        if(preg_match('/^(zone_name)/',$key)){
            $current_zone = $value;
            $center[$current_zone] = array('zone_name'=>$value,'subdata'=>'');
        }elseif(preg_match('/^(institute_name)/',$key)){
            $contract[$current_section][subdata][Institute] = $value;
        }
    }