Announcement

Collapse
No announcement yet.

[RESOLVED] Need help with an array issue

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • [RESOLVED] Need help with an array issue

    So, I have a JSON object that I am casting to an array with json_decode. The thing is the array is multi-layered, but not as a multi-dimensional array would be... Here is an example though this is still in object format.

    Code:
    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 multi-dimensional array $myArray['detail']['ordnbr']
    Nothing I have tried works. Can someone please point me in the right direction.
    Thanks in advance.

  • #2
    json_decode() returns an object, not an array. You would access it like this:
    PHP Code:
    $myArray->detail[0]->ordnbr 
    If you want an array instead, pass TRUE as the second parameter for json_decode() like so:
    PHP Code:
    $myArray json_decode$theJSONTRUE ); 
    Then you can access like so:
    PHP Code:
    $myArray['detail'][0]['ordnbr'
    json_decode() Specifications

    Comment


    • #3
      Thank you ScareCrowe... I should have known that. I think I had just gotten so aggravated with this that I couldn't think it through clearly...

      Comment

      Working...
      X