1

Building App in Ionic and webservices in PHP. My issue is I am getting an object from the app while hitting the api.

[option] => stdClass Object
        (
            [228] => 19
            [229] => 22
            [230] => 24
            [231] => 26
        )

In php I convert this object to array using the following code..

$option = (array)$this->request->post['option'];

Then I check its type, it is an array. But When I am trying to get the value of $option[228] it returns me an error "Undefined Offset:228". The issue may be it is finding the index value 228 rather than matching the associative key value..

How I can get the value using these numeric keys??

Simerjit Parmar
  • 808
  • 1
  • 7
  • 22

2 Answers2

1

Alternate workaround is you can prepare array and then access it's key -> value pair if you require it's keys as well then, like below...

$option = array_combine(array_keys((array)$option), array_values((array)$option));

Then you can access it's elements like $option[228]

himeshc_IB
  • 853
  • 4
  • 10
0

You can use array_values($option) to reset the keys.

[
  0 => 29
  1 => 22
  2 => 24
  3 => 26
]

Or use the object like this:

$option->{228}

xpuc7o
  • 333
  • 3
  • 15