Q:

How to divide JSON data into multiple JSON objects with our own key-pair values in PHP?

I am asking question for the first time here. Sorry in advance if the format isn't perfect.
I have a file named data.json in which data are stored in this way:
{
 "iPhone": 80,
 "MacBook": 120,
 "AirPods": 112
}

But I want to create my own key-value pair like this: 
[
 {
  class: "iPhone",
  quantity: 80
 },
 {
  class: "MacBook",
  quantity: 120
 },
 {
  class: "AirPods",
  quantity: 112
 }
]

A:

$temp = json_decode($json_str,true);

$output = [];
foreach($temp as $key => $value){
   $output[] = ['class'=>$key,'quantity'=>$value];
}

echo json_encode($output);

