0

I have an array variable $array like this

$array = Array
         (
            [results] => stdClass Object
            (
              [successc] => stdClass Object
              (
                [926] => stdClass Object
                    (
                        [transaction_id] => xx
                        [transaction_code] => xx
                        [status] => xx
                        [amount] => 5
                    )
              )

              [success] => Array
              (
                [0] => Successful transaction
              )

           )

        )

I want to access the transaction_id element. The 926 is a variable value. It could very well be 927 or 928. It comes from another object $cc. Would it be correct to access the transaction_id using the following code?

$x = $cc->id;
$transaction_id = $array['results']->successc->{$x}->transaction_id;
LF00
  • 27,015
  • 29
  • 156
  • 295
jai
  • 391
  • 2
  • 6
  • 14
  • have you tried this code ? what is error in this code ? – Ahmed Ginani May 17 '17 at 07:12
  • I have not tried this code. I know it is unfair to ask doubts. But I am operating on a live server and do not want to mess up things – jai May 17 '17 at 07:16
  • I think it should work. Just try once the same instance in your local system and then add it in your production server. – prava May 17 '17 at 07:17

1 Answers1

2

Your approach is not bad, but the code structure looks more like an array.

To convert to a full array, you can encode to json en decode to array.

 $array = json_decode(json_encode($array), true);

When TRUE, returned objects will be converted into associative arrays.

So you can access each level of $array as array element.

schellingerht
  • 5,726
  • 2
  • 28
  • 56