subhra subhra - 2 months ago 10
PHP Question

Can not sort the array into proper format using PHP

I have some issue. I need to resize my array in a proper format using PHP. I am giving my data below.

$result = Array
(
[0] => Array
(
[cat_id] => 2
[cat_name] => spirit
[subcat_id] => 20
[subcat_name] => pizza
)

[1] => Array
(
[cat_id] => 2
[cat_name] => spirit
[subcat_id] => 22
[subcat_name] => wine
)

[2] => Array
(
[cat_id] => 3
[cat_name] => Food
[subcat_id] => 23
[subcat_name] => pakhal
)

[3] => Array
(
[cat_id] => 3
[cat_name] => Food
[subcat_id] => 24
[subcat_name] => fuddy
)

);


The above array should be arranged in the below format.

$data = [{
"category" : "Spirits",
'cat_id' : 2,
"subCatgory" : [{
"id" : "20",
"name" : "pizza"
},
{
"id" : "22",
"name" : "wine"
}]
},
{
"category" : "Food",
"cat_id" : "3",
"subCatgory" : [{
"id" : "23",
"name" : "pakhal"
},
{
"id" : "24",
"name" : "fuddy"
}]
}]


I did like below but its not giving the required format.

<?php
$result=array(array('cat_id'=>'2','cat_name'=>'spirit','subcat_id'=>'20','subcat_name'=>'pizza'),array('cat_id'=>'2','cat_name'=>'spirit','subcat_id'=>'22','subcat_name'=>'wine'),array('cat_id'=>'3','cat_name'=>'Food','subcat_id'=>'23','subcat_name'=>'pakhal'),array('cat_id'=>'3','cat_name'=>'Food','subcat_id'=>'24','subcat_name'=>'fuddy'));

for($i=0;$i<count($result);$i++){
if($i==0){
$data[]=array("category"=>$result[$i]['cat_name'],"cat_id"=>$result[$i]['cat_id'],"subCatgory"=>array(array("id"=>$result[$i]['subcat_id'],"name"=>$result[$i]['subcat_name'])));
}else{

for($j=0;$j<count($data);$j++){
if($data[$j]['cat_id']==$result[$i]['cat_id']){
for($k=0;$k<count($data[$j]['subCatgory']);$k++){
if($data[$j]['subCatgory'][$k]['id']==$result[$i]['subcat_id']){
$find=1;
break;
}else{
$find=0;
}

}
if($find==0){
$data[$j]['subCatgory'][]=array("id"=>$result[$i]['subcat_id'],"name"=>$result[$i]['subcat_name']);
}
if($find==1){
break;
}

}else{
$data[]=array("category"=>$result[$i]['cat_name'],"cat_id"=>$result[$i]['cat_id'],"subCatgory"=>array(array("id"=>$result[$i]['subcat_id'],"name"=>$result[$i]['subcat_name'])));

}

}
}
}
echo json_encode($data);
?>


The above code is giving the wrong format. Please help me.

Answer

Here I have write your solution :

    <?php   
    $result = array(
        array( 
            'cat_id' => '2',
            'cat_name' => 'spirit',
            'subcat_id' => '20',
            'subcat_name' => 'pizza'
        ),
        array(
            'cat_id' => '2',
            'cat_name' => 'spirit',
            'subcat_id' => '20',
            'subcat_name' => 'pizza'
        ),
        array(
            'cat_id' => '2',
            'cat_name' => 'spirit',
            'subcat_id' => '22',
            'subcat_name' => 'wine'
        ),
        array(
            'cat_id' => '3',
            'cat_name' => 'Food',
            'subcat_id' => '23',
            'subcat_name' => 'pakhal'
        ),
        array(
            'cat_id' => '3',
            'cat_name' => 'Food',
            'subcat_id' => '24',
            'subcat_name' => 'fuddy'
        )
    );

$finalArr = array();
foreach($result as $arr) {
    $finalArr[] = array(
        'category' => $arr['cat_name'],
        'cat_id' => $arr['cat_id']
    );
    $subCatArr[$arr['cat_id']][] = array(
        'id' => $arr['subcat_id'],
        'name' => $arr['subcat_name']
    ); 
}

foreach($subCatArr as $key => $sub) {
    $subCatArr[$key] = array_map("unserialize", array_unique(array_map("serialize", $sub)));
}

$finalArr = array_map("unserialize", array_unique(array_map("serialize", $finalArr)));



foreach($finalArr as $key => $a) {
    if(array_key_exists($a['cat_id'],$subCatArr)) {
        $finalArr[$key]['subCatgory'] = array_values($subCatArr[$a['cat_id']]);
    }
}

echo json_encode(array_values($finalArr));

Please try this.

Comments