0

So, I have a JSON object that I'm casting to an array with json_decode(). The thing is the array is multidimensional, but not as a multi-dimensional array would be.

Here is an example though this is still in object format:

object(stdClass)#2916 (15) {
["trknbr"]=>
string(12) "34563"
["siteid"]=>
string(3) "My Other Site"
["flatrate"]=>
string(1) "0"
["ratemile"]=>
string(1) "0"
["miles"]=>
string(3) "244"
["covered"]=>
string(1) "N"
["dateship"]=>
string(10) "0000-00-00"
["trkco"]=>
string(0) ""
["totalchrg"]=>
string(1) "0"
["fuelchrg"]=>
string(1) "0"
["tarp"]=>
string(1) "N"
["shipdirection"]=>
string(3) "out"
["comments"]=>
NULL
["custname"]=>
string(25) "Another Customer"
["detail"]=>
array(1) {
  [0]=>
  object(stdClass)#2830 (7) {
    ["trknbr"]=>
    string(12) "34563"
    ["ordnbr"]=>
    string(6) "34578"
    ["dtrelease"]=>
    string(10) "2015-06-08"
    ["custid"]=>
    string(6) "wwweee"
    ["shipto"]=>
    string(12) "Another City"
    ["slsperid"]=>
    string(3) "RRR"
    ["comments"]=>
    string(31) "DO NOT SHIP BEFORE 6/8
    $  769P"
  }

Note how the detail row is its own array. I need to access a value from the ordnbr field which is under detail.

I have tried everything, I know including trying to access it as if it were a multidimensional array:

$myArray['detail']['ordnbr']

Nothing I have tried works.

Can someone please point me in the right direction.

Rizier123
  • 58,877
  • 16
  • 101
  • 156
trouble706
  • 48
  • 3
  • 15
  • This question is **not** a duplicate of the question provided. This has to do with the second parameter of `json_decode` and returning a \stdClass or an associative array. The referenced question does not answer this question whatsoever. – NobleUplift Jun 12 '15 at 18:00
  • trouble706 use `json_decode($jsonString, true);` and it will work. – NobleUplift Jun 12 '15 at 18:00
  • Thank you NobleUplift. I am trying that now, though I am a complete newbie to json... – trouble706 Jun 12 '15 at 18:12
  • Be warned that if you do `json_decode` to associative arrays, you will lose the distinction between `{}` and `[]`. So if you plan on using this JSON in Java or strict JavaScript, you need to make sure you have no empty arrays or decode to a class and then convert that class to an array. – NobleUplift Jun 12 '15 at 18:16
  • Want to unlock this question @Rizier123? – NobleUplift Jun 12 '15 at 18:17
  • @NobleUplift The question is asking, how he can access an element in an array and or object. The dupe exactly show how this is done – Rizier123 Jun 12 '15 at 18:21
  • No, he's asking how to decode a JSON object into an associative array. Nowhere in the question you marked as the original can the word "JSON" be found. – NobleUplift Jun 12 '15 at 18:24
  • @NobleUplift I am missing something, I guess... I assumed that you meant I would need to call json_decode($jsonString, true); with my array name such as $myArray = json_decode($jsonString, true); But when doing that and viewing Developer Tools in Chrome, that returns NULL.... – trouble706 Jun 12 '15 at 18:26
  • What are you viewing in developer tools? Are you calling this as AJAX? I would use var_export if it is, var_dump if it is not AJAX, to confirm that it is coming back as `NULL`. Also, if I don't respond I'm at lunch. – NobleUplift Jun 12 '15 at 18:38
  • Also, put your JSON string in your question if you can. – NobleUplift Jun 12 '15 at 18:39
  • Not AJAX, it is PHP. This is what one line that is returned looks like, if I can post it..[detail] => Array ( [0] => stdClass Object ( [trknbr] => rew2015-4345 [ordnbr] => TD3449 [dtrelease] => 2015-06-04 [custid] =>CUST [shipto] => Some Town, ST [slsperid] => AN [comments] => Truck # 2 of 5 $2360 ) ) ) [1] => stdClass Object ( [trknbr] => rew2015-4345 [siteid] => SRE [flatrate] => 0 [ratemile] => 0 [miles] => 244 [covered] => N [dateship] => 0000-00-00 [trkco] => [totalchrg] => 0 [fuelchrg] => 0 [tarp] => N [shipdirection] => out [comments] => [lastname] => Smith – trouble706 Jun 12 '15 at 18:53
  • @NobleUplift What I need to do is in a foreach loop, get all values of the ordnbr field. That is what I am having a problem with. If I can get that solved, I think I will be ok... Thank you so much for looking at this with me. – trouble706 Jun 12 '15 at 18:55
  • I meant why are you using developer tools to see the results of PHP, a server-side language, so I thought you were trying to decode the JSON in an AJAX call. Can you add the raw JSON string to your question, so that I can see what might be going wrong? – NobleUplift Jun 12 '15 at 20:20
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/80431/discussion-between-trouble706-and-nobleuplift). – trouble706 Jun 12 '15 at 20:21

0 Answers0