如何在数组PHP中设置重复索引的最大数量

let's say I have this array :

$array = [  
{
  "id": 3175,
  "shape_id": 307,
  "shape_pt_lat": "-6.9257591914493",
  "shape_pt_lon": "107.60664492839",
  "shape_pt_sequence": 2,
  "shape_dist_travel": "",
  "jalur": "146-147",
  "distance": 0.16133102419521
},
{
  "id": 3180,
  "shape_id": 308,
  "shape_pt_lat": "-6.9257591914493",
  "shape_pt_lon": "107.60664492839",
  "shape_pt_sequence": 2,
  "shape_dist_travel": "",
  "jalur": "147-146",
  "distance": 0.16133102419521
},
{
  "id": 3176,
  "shape_id": 307,
  "shape_pt_lat": "-6.9257911430218",
  "shape_pt_lon": "107.6069118082",
  "shape_pt_sequence": 3,
  "shape_dist_travel": "",
  "jalur": "146-147",
  "distance": 0.16415806438464
},
{
  "id": 3179,
  "shape_id": 308,
  "shape_pt_lat": "-6.9257911430218",
  "shape_pt_lon": "107.6069118082",
  "shape_pt_sequence": 1,
  "shape_dist_travel": "",
  "jalur": "147-146",
  "distance": 0.16415806438464
},
{
  "id": 3174,
  "shape_id": 307,
  "shape_pt_lat": "-6.9257312336896",
  "shape_pt_lon": "107.60638207192",
  "shape_pt_sequence": 1,
  "shape_dist_travel": "",
  "jalur": "146-147",
  "distance": 0.16421114665333
} ]

how can I move those array to new array that only contain max two index that has the same value in shape_id.

so, the result I expected is :

    $array = [  
{
  "id": 3175,
  "shape_id": 307,
  "shape_pt_lat": "-6.9257591914493",
  "shape_pt_lon": "107.60664492839",
  "shape_pt_sequence": 2,
  "shape_dist_travel": "",
  "jalur": "146-147",
  "distance": 0.16133102419521
},
{
  "id": 3180,
  "shape_id": 308,
  "shape_pt_lat": "-6.9257591914493",
  "shape_pt_lon": "107.60664492839",
  "shape_pt_sequence": 2,
  "shape_dist_travel": "",
  "jalur": "147-146",
  "distance": 0.16133102419521
},
{
  "id": 3176,
  "shape_id": 307,
  "shape_pt_lat": "-6.9257911430218",
  "shape_pt_lon": "107.6069118082",
  "shape_pt_sequence": 3,
  "shape_dist_travel": "",
  "jalur": "146-147",
  "distance": 0.16415806438464
},
{
  "id": 3179,
  "shape_id": 308,
  "shape_pt_lat": "-6.9257911430218",
  "shape_pt_lon": "107.6069118082",
  "shape_pt_sequence": 1,
  "shape_dist_travel": "",
  "jalur": "147-146",
  "distance": 0.16415806438464
}]

Please notice the last index array that have shape_id value = 307 did not copied because the new array aready have two object that have 307 earlier.

thank in advance !