0

This is my array

array(
      ['studentId'=>"M100030","isbn"=>"0199535566",'price'=>"15.00"],
      ['studentId'=>"M100030","isbn"=>"0199535566",'price'=>"13.50"],
      ['studentId'=>"M100030","isbn"=>"0143105426",'price'=>"20.00"],
      ['studentId'=>"M100035","isbn"=>"1604501480",'price'=>"21.00"],
      ['studentId'=>"M100035","isbn"=>"1604501480",'price'=>"23.00"],
      ['studentId'=>"M100035","isbn"=>"0199535566",'price'=>"14.00"],
      ['studentId'=>"M103233","isbn"=>"0061964360",'price'=>"18.50"],
    );

I want to group the studentId and isbn then sum up the price to JSON

{
   "M100030":{
      "0199535566":{
         "amount":"28.50"
      },
      "0143105426":{
         "amount":"20.00"
      }
   },
   "M100035":{
      "1604501480":{
         "amount":"44.00"
      },
      "0199535566":{
         "amount":"14.00"
      }
   },
   "M103233":{
      "0061964360":{
         "amount":"18.50"
      }
   }
}

The method I tried so far to group the studentId and isbn, but not able to get the expected result. Anyone can correct my code?

foreach ($check['rentout'] as $key=> $values)
{

    $keys = $values['studentId'];
    if (!array_key_exists($keys,$trx)){
      $trx[$keys] = array('isbn'=>$values['isbn'],'amount'=>$values['price']);
    }else{
      $trx[$keys]['amount'] = $trx[$keys]['amount']+$values['price'];
    }

}
1

1 Answer 1

1
$students = array(
      ['studentId'=>"M100030","isbn"=>"0199535566",'price'=>"15.00"],
      ['studentId'=>"M100030","isbn"=>"0199535566",'price'=>"13.50"],
      ['studentId'=>"M100030","isbn"=>"0143105426",'price'=>"20.00"],
      ['studentId'=>"M100035","isbn"=>"1604501480",'price'=>"21.00"],
      ['studentId'=>"M100035","isbn"=>"1604501480",'price'=>"23.00"],
      ['studentId'=>"M100035","isbn"=>"0199535566",'price'=>"14.00"],
      ['studentId'=>"M103233","isbn"=>"0061964360",'price'=>"18.50"],
    );

I Created variable $res and I looped over $students and get the $student_id and do a test if there is no isbn for this student, If So set their amount to the initial value, and loop again if we find it again accumulate its prices to amount, and do this again and again to all students in the array.

$res = [];
foreach ($students as $key => $student) {
    $student_id = $student['studentId'];
    if (!isset($res[$student_id][$student["isbn"]])){
      $res[$student_id][$student["isbn"]]["amount"] = $student['price'];
    }else{
        $res[$student_id][$student["isbn"]]["amount"] += $student['price'];
    }
}
echo "<pre>";
echo json_encode($res, JSON_PRETTY_PRINT);

Print(as excpected)

{
    "M100030": {
        "0199535566": {
            "amount": 28.5
        },
        "0143105426": {
            "amount": "20.00"
        }
    },
    "M100035": {
        "1604501480": {
            "amount": 44
        },
        "0199535566": {
            "amount": "14.00"
        }
    },
    "M103233": {
        "0061964360": {
            "amount": "18.50"
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.