skip to Main Content

i have this type of data store in mysql, how can i echo only the amount value from this?

{"1":{"amount":"700","date":"2022-10-31","amount_discount":"0","amount_fine":"0","description":"","collected_by":"John Doe","payment_mode":"Cash","received_by":"12","inv_no":1},"2":{"amount":"300","date":"2022-12-17","description":" Paid by: Jane Doe","amount_discount":0,"amount_fine":"0","payment_mode":"upi","received_by":"23","inv_no":2}}

this code looks like json data, if yes then should i be using jquery to echo the values or is there a way to do it in php?

2

Answers


  1. You can use the json_decode function to turn it into a PHP object and then access its properties. For your example, you can do this to get the "amount" property:

    <?php
    $js='{"1":{"amount":"700","date":"2022-10-31","amount_discount":"0","amount_fine":"0","description":"","collected_by":"John Doe","payment_mode":"Cash","received_by":"12","inv_no":1},"2":{"amount":"300","date":"2022-12-17","description":" Paid by: Jane Doe","amount_discount":0,"amount_fine":"0","payment_mode":"upi","received_by":"23","inv_no":2}}';
    echo json_decode($js)->{"1"}->amount;
    ?>
    
    Login or Signup to reply.
  2. $data = json_decode('{"1":{"amount":"700","date":"2022-10-31","amount_discount":"0","amount_fine":"0","description":"","collected_by":"John Doe","payment_mode":"Cash","received_by":"12","inv_no":1},"2":{"amount":"300","date":"2022-12-17","description":" Paid by: Jane Doe","amount_discount":0,"amount_fine":"0","payment_mode":"upi","received_by":"23","inv_no":2}}');
    Ex1: ((array) $data)[1]->amount
    Ex2: $data->{'1'}->amount]);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search