0

Just a real quick question please, I have this string that came from my query

I am able to display the string using xx = dd($sumx) which gave me the string below:

string(124) "[{"total":-4107717.58,"alerx":4,"currentYear":-4107717.58,"lastYear":0,"date":2015,"value":{"debit":0,"credit":4107717.58}}]"

in order for me to access it via '->' notation, like an object. I convert it using json_decode()

I echoed it again and gives me this object format:

array(1) {
[0]=>
object(stdClass)#508 (6) {
  ["total"]=>
  float(-4107717.58)
  ["alerx"]=>
  int(4)
  ["currentYear"]=>
  float(-4107717.58)
  ["lastYear"]=>
  int(0)
  ["date"]=>
  int(2015)
  ["value"]=>
  object(stdClass)#509 (2) {
    ["debit"]=>
    int(0)
    ["credit"]=>
    float(4107717.58)
  }
 }
}

when I tried to access $sumx->value it then gives me a `Trying to get property of non-object

I tried accessing it via $sumx[0]->value same error, I also tried validating the string to http://jsonlint.com/ and it says its valid. Can someone point out to me whats wrong please. Thanks for the time and have a good day.

melvnberd
  • 3,093
  • 6
  • 32
  • 69

1 Answers1

1

Try like below:-

    <?php
$data = '[{"total":-4107717.58,"alerx":4,"currentYear":-4107717.58,"lastYear":0,"date":2015,"value":{"debit":0,"credit":4107717.58}}]';
$new_array = json_decode($data);
echo "<pre/>";print_r($new_array);
echo "<pre/>";print_r($new_array['0']->value);
echo "<pre/>";print_r($new_array['0']->value->debit);
echo "<pre/>";print_r($new_array['0']->value->credit);
?>

Output:- https://eval.in/381395

Note:- change is $new_array['0']->value only.

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
  • It worked sir @anantkumarsingh, Thank you very much for the effort. :) I think its may error too, since i assigned the converted object into the variable $xx but still I tried to access the raw string data from $sumx -_-.. some programmer error, sorry for that. but takes me forever to figure out :) – melvnberd Jun 15 '15 at 01:30
  • Always welcome. love to solve any once problem. have fun.:):) – Alive to die - Anant Jun 15 '15 at 01:32