-2

I am receiving the output below from a web service. (Output shown is via var_dump(json_decode($json,true)); To get data from the latter part of the object I can use, for example:

$obj = json_decode($json);
$myvalue=$obj->{'CurrentAccountNumber'};

but I also need to get data from the arrays at the beginning of the object (via php). What is the right syntax to use to get/echo the array data using php? Thank you!

OUTPUT FROM WEB SERVICE:

array(30) { ["Accounts"]=> array(2) { [0]=> array(4) { ["AccountNumber"]=> string(9) "123456789" ["CurrentBalance"]=> float(455.17) ["FullName"]=> string(8) "JOHN DOE" ["CodeNum"]=> string(3) "A34" } [1]=> array(4) { ["AccountNumber"]=> string(9) "123456788" ["CurrentBalance"]=> float(67.22) ["FullName"]=> string(8) "JOHN DOE" ["CodeNum "]=> string(3) "B82" } }

HOW DO I GET DATA FROM THE ARRAYS ABOVE THIS LINE?

["CurrentAccountNumber"]=> string(9) "123456789" ["CurrentBalance"]=> float(455.17) ["CustomMonth"]=> bool(true) ["CustomMonthAmt"]=> float(488.17) ["CustomMonthLength"]=> int(0) ["DateOfService"]=> string(10) "2013-06-15" ["EighteenMonth"]=> bool(false) ["EighteenMonthAmt"]=> int(0) ["ExpectedLastPaymentAmount"]=> float(0) ["ExpectedLastPaymentDate"]=> string(0) "" ["FullName"]=> string(8) "JOHN DOE" ["Email"]=> string(11) "myemail.com" ["LastFourPhone"]=> string(4) "5128" ["LastPaymentAmount"]=> float(4205.83) ["LastPaymentDate"]=> string(10) "2015-04-28" ["NextPaymentAmount"]=> float(0) ["NextPaymentDate"]=> string(0) "" ["Category"]=> int(0) ["InsPlanStartDate"]=> string(10) "2016-10-08" ["PrimaryPhoneNumber"]=> string(10) "5555555128" ["SixMonth"]=> bool(false) ["SixMonthAmt"]=> int(0) ["CodeNum"]=> string(3) "A34" ["TwelveMonth"]=> bool(false) ["TwelveMonthAmt"]=> int(0) ["TwentyFourMonth"]=> bool(false) ["TwentyFourMonthAmt"]=> int(0) ["UseInfoOnFile"]=> bool(true) ["ZipCode"]=> string(5) "12345" }

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
RWilliams
  • 1
  • 1
  • 1
  • First you decode your json data to an array and after that you decode it to an object. Also you show us two different data outputs from the same variable. So you are mixing some stuff up here. – Rizier123 Oct 07 '16 at 14:38
  • Yes, what is returned in the json object is arrays in the first part of the object and a string in the second part. Thank you for your reply. – RWilliams Oct 10 '16 at 13:47

2 Answers2

1

Use $obj->CurrentAccountNumber syntax instead.

You can also use it with the typical array syntax $obj['CurrentAccountNumber'] if you decode the json into an array instead of an object, like you are doing in the var_dump.

To do this change this

$obj = json_decode($json);

by this

$array = json_decode($json, true);
jorgonor
  • 1,679
  • 10
  • 16
0

json_decode($json) makes a StdClass object out of the JSON data (if you don't set the 2nd parameter to true, see http://php.net/manual/en/function.json-decode.php), so you can access its property with $obj->propertyName and iterate other that property if it's an array (using foreach or so).

If you want to keep an associative array, leave the 2nd parameter json_decode($json, true); and access values like any other associative array with $obj['CurrentAccountNumber'] (but here, variables names are confusing).

Xenos
  • 3,351
  • 2
  • 27
  • 50