0

I mentioned my php array object following below,

$arr ='[
{
    "id": 4667,
    "brand": "Michelin",
    "model": "Pilot Super Sport",
    "width": "255",
    "height": "35",
    "rim": "19",
},
{
    "id": 4668,
    "brand": "Michelin",
    "model": "Pilot Super Sport",
    "width": "275",
    "height": "35",
    "rim": "19",
},
{
    "id": 4669,
    "brand": "Pirelli",
    "model": "Zero",
    "width": "255",
    "height": "35",
    "rim": "19",
},
{
    "id": 4670,
    "brand": "Pirelli",
    "model": "Zero",
    "width": "275",
    "height": "35",
    "rim": "19",
}]';

I want to split array into front and rear separate like following below based on width.

$front = '[
{
    "id": 4667,
    "brand": "Michelin",
    "model": "Pilot Super Sport",
    "width": "255",
    "height": "35",
    "rim": "19",
},
{
    "id": 4669,
    "brand": "Pirelli",
    "model": "Zero",
    "width": "255",
    "height": "35",
    "rim": "19",
}]';

$rear = '[
{
    "id": 4668,
    "brand": "Michelin",
    "model": "Pilot Super Sport",
    "width": "275",
    "height": "35",
    "rim": "19",
},
{
    "id": 4670,
    "brand": "Pirelli",
    "model": "Zero",
    "width": "275",
    "height": "35",
    "rim": "19",
}]';

It's there any way php can compare each object width value and group them which is match. please advise. thanks for your valuable time.

Syscall
  • 19,327
  • 10
  • 37
  • 52
Haja
  • 74
  • 2
  • 11
  • Based on what are they classified as front or rear? – Geert Oct 27 '21 at 11:00
  • Please, show us your actual code. Thank you. – Syscall Oct 27 '21 at 11:02
  • 1
    Note: the given code is not a valid PHP code. – Syscall Oct 27 '21 at 11:02
  • Hello Syscall please wait i will update my php code – Haja Oct 27 '21 at 11:04
  • Hello Geert, Based on Tire width we can split front & rear – Haja Oct 27 '21 at 11:05
  • code is not valid it should be like: $arr='[{ "id": 4667, "brand": "Michelin", "model": "Pilot Super Sport", "width": "255", "height": "35", "rim": "19" }, { "id": 4668, "brand": "Michelin", "model": "Pilot Super Sport", "width": "275", "height": "35", "rim": "19" }, { "id": 4669, "brand": "Pirelli", "model": "Zero", "width": "255", "height": "35", "rim": "19" }, { "id": 4670, "brand": "Pirelli", "model": "Zero", "width": "275", "height": "35", "rim": "19" }]'; – PHP Hupp Technologies Oct 27 '21 at 11:22

2 Answers2

1

I think you can use below code:

$arr='[{
    "id": 4667,
    "brand": "Michelin",
    "model": "Pilot Super Sport",
    "width": "255",
    "height": "35",
    "rim": "19"
}, {
    "id": 4668,
    "brand": "Michelin",
    "model": "Pilot Super Sport",
    "width": "275",
    "height": "35",
    "rim": "19"
}, {
    "id": 4669,
    "brand": "Pirelli",
    "model": "Zero",
    "width": "255",
    "height": "35",
    "rim": "19"
}, {
    "id": 4670,
    "brand": "Pirelli",
    "model": "Zero",
    "width": "275",
    "height": "35",
    "rim": "19"
}]'; 
$front=[];
$rear=[];
foreach(json_decode($arr,true) as $key=>$val)
{
  
  if($val['width']==255)
  {
    $front[]=$val;
  }
  else{
    $rear[]=$val;
  }
}  

$front=json_encode($front);
$rear=json_encode($rear);
  • Thanks for your replay i need to split array based on width – Haja Oct 27 '21 at 11:16
  • You can pass argument based on width in array_chunk. – PHP Hupp Technologies Oct 27 '21 at 11:17
  • if total elemets are 10 then you need 5 in front and 5 in rear, right? – PHP Hupp Technologies Oct 27 '21 at 11:19
  • Sometime we will get available product 3 front tires & 5 rear tires, in database return all 8 elements in single array i need to display product front and rear separate, we can identify front and rear using width only – Haja Oct 27 '21 at 11:23
  • I have updated code please check. you can put condition for front as per requirement. – PHP Hupp Technologies Oct 27 '21 at 11:39
  • Thanks for your effort, but i am sorry thats not working for me. in following link they did that using javascript i need to do something like that in php. ref: https://stackoverflow.com/questions/46794232/group-objects-by-multiple-properties-in-array-then-sum-up-their-values – Haja Oct 27 '21 at 11:49
  • Did you correct your JSON? it should be like:$arr='[{ "id": 4667, "brand": "Michelin", "model": "Pilot Super Sport", "width": "255", "height": "35", "rim": "19" }, { "id": 4668, "brand": "Michelin", "model": "Pilot Super Sport", "width": "275", "height": "35", "rim": "19" }, { "id": 4669, "brand": "Pirelli", "model": "Zero", "width": "255", "height": "35", "rim": "19" }, { "id": 4670, "brand": "Pirelli", "model": "Zero", "width": "275", "height": "35", "rim": "19" }]'; – PHP Hupp Technologies Oct 27 '21 at 11:55
  • For front what are the width we need to consider or its fixed 255? – PHP Hupp Technologies Oct 27 '21 at 11:55
  • Yes i update corrections, thank you so much.. & 255 is not fixed value it will change based on tire modal but every JSON will have only two different width size – Haja Oct 27 '21 at 12:01
  • Yeah so you need to make dynamic check here if($val['width']==255) as per your requirement – PHP Hupp Technologies Oct 27 '21 at 12:02
  • Sorry for late reply, PHP Hupp Technologies.. Yeah ive got spark after look into your code and finally i get what i need.. please check if there's any way to make that code optimize please advise – Haja Oct 27 '21 at 12:19
  • $all = []; $front = []; $rear = []; for($i=0; $iwidth); } $filter = array_unique($all); $max = max($filter); $min = min($filter); for($j=0; $jwidth == $max){ array_push($rear, $arr[$j]); } else if($arr[$j]->width == $min){ array_push($front, $arr[$j]); } else { echo 'Not Match'; } } echo ""; echo ""; – Haja Oct 27 '21 at 12:20
0

This is works for me but need to simplify

 $all = []; $front = []; $rear = [];

        for($i=0; $i<count($arr); $i++){
            array_push($all, $arr[$i]->width);
        }

        $filter = array_unique($all);
        $max = max($filter);
        $min = min($filter);
        
        for($j=0; $j<count($arr); $j++){
            if($arr[$j]->width == $max){
                array_push($rear, $arr[$j]);
            } else if($arr[$j]->width == $min){
                array_push($front, $arr[$j]);
            } else {
                echo 'Not Match';
            }
        }

        echo "<script>console.log(".json_encode($front).")</script>";
        echo "<script>console.log(".json_encode($rear).")</script>";
Haja
  • 74
  • 2
  • 11