1

This is the example of an Array & Object:

    $LObj = [
            0 => (object) [ 
                0 => 1,
                1 => 'a',
                3 => 'str',
                ],
            1 => (object) [ 
                0 => 2,
                1 => 'a',
                3 => 'str',
                ],
            2 => (object) [ 
                0 => 3,
                1 => 'a',
                3 => 'str',
                ],
            1 => (object) [ 
                0 => 4, //printme
                1 => 'a',
                3 => 'str',
                ],
            'bonus' => (object) [ 
                0 => '4',
                '1' => 1,
                'bonus' => 'str',
                ],
            ];
         var_dump($LObj['bonus']);
         echo $LObj['bonus']->0; // #1 how do you display '4' value? 
         echo $LObj['bonus']->1; // #2 how do you display 1 value? 
         echo $LObj['bonus']->bonus;  // #3 is 'str' 

I wanted to display each #1, #2, #3 as shown above using a single line of code (like #3) w/o using any loop (foreach). #3 is displayed correctly, problem is #1 & #2. I've been seeking answers but couldn't find one. Any help would be highly appreciated, thanks.

Ray Macz
  • 85
  • 2
  • 9
  • 1
    If this comes from `json_decode` then use `true` as second arg to get an array. – AbraCadaver May 14 '18 at 20:12
  • Thanks. but it doesn't come from `json_decode` One that you gave a link, I can't make it work. I just wanted to ask if this can be done using single line of code in PHP. The example you showed earlier, it doesn't work. Again Thanks. – Ray Macz May 14 '18 at 20:36
  • `echo array_values((array)$LObj['bonus'])[0];` or `echo json_decode(json_encode($LObj['bonus']), true)[0];` but easier just to do: `$LObj = json_decode(json_encode($LObj), true);` – AbraCadaver May 14 '18 at 20:39
  • `echo array_values((array)$LObj['bonus'])[0];` Now that works! Much appreciated mate. Thank you for your patience. – Ray Macz May 14 '18 at 20:46

0 Answers0