-1

please I need help on how to read key value pair from JSON data. Below is my JSON data

[{"DATE":"2023-12-30","ID":"50","CATEGORY":"CHECKER CARDS","SERVICE":"WASSCE","AMOUNT":"100.00","CENTER":"TABORA"},{"DATE":"2023-12-28","ID":"49","CATEGORY":"TYPING","SERVICE":"Premium Typing","AMOUNT":"75.00","CENTER":"TABORA"},{"DATE":"2023-12-20","ID":"48","CATEGORY":"PRINTING","SERVICE":"A4 Color Print","AMOUNT":"9.00","CENTER":"TABORA"}]

Below is the code I used:

       $table_data = json_encode($_POST['transfer']);
       //$table_data = $_POST['transfer'];
        echo "JSON Encoded ".$table_data;
        echo "<br><br>";

        $items = json_decode($table_data,true);
        //echo "JSON Decoded ".$items;
        print_r($items);
        //var_dump($items);

        echo "<br><br>";

        foreach($items as $item){
           echo $item->DATE; 
        }
  • 1
    You used `json_decode` with second parameter set to true, which means all objects will be converted into associative arrays. So you can't use object property access syntax any more, you need to use array access syntax then. – CBroe Aug 18 '23 at 13:53
  • Please can you direct me as to the best course of action? – Abraham Cudjoe Aug 18 '23 at 13:55
  • 3
    _Don't_ set the second parameter to true ...? – CBroe Aug 18 '23 at 14:03
  • I guess more generally than the JSON issue itself - why are you encoding POST data *into* JSON and then immediately decoding it again? If it's already in a PHP data structure, just leave it like that. Especially if you're now having trouble accessing it. – iainn Aug 18 '23 at 14:06
  • Agreed...encoding and then decoding it again appears to be entirely redundant in this case – ADyson Aug 18 '23 at 15:45

0 Answers0