0

My API call returns following data in Codeigniter:

Pronamic\Twinfield\VatCode\VatCode Object
(
    [code:Pronamic\Twinfield\VatCode\VatCode:private] => IH
    [name:Pronamic\Twinfield\VatCode\VatCode:private] => BTW 21%
)
Pronamic\Twinfield\VatCode\VatCode Object
(
    [code:Pronamic\Twinfield\VatCode\VatCode:private] => IL
    [name:Pronamic\Twinfield\VatCode\VatCode:private] => BTW 6%
)
Pronamic\Twinfield\VatCode\VatCode Object
(
    [code:Pronamic\Twinfield\VatCode\VatCode:private] => IN
    [name:Pronamic\Twinfield\VatCode\VatCode:private] => BTW 0%
)
Pronamic\Twinfield\VatCode\VatCode Object
(
    [code:Pronamic\Twinfield\VatCode\VatCode:private] => VH
    [name:Pronamic\Twinfield\VatCode\VatCode:private] => BTW 21%
)
Pronamic\Twinfield\VatCode\VatCode Object
(
    [code:Pronamic\Twinfield\VatCode\VatCode:private] => VL
    [name:Pronamic\Twinfield\VatCode\VatCode:private] => BTW 6%
)
Pronamic\Twinfield\VatCode\VatCode Object
(
    [code:Pronamic\Twinfield\VatCode\VatCode:private] => VN
    [name:Pronamic\Twinfield\VatCode\VatCode:private] => BTW 0%
)

My (simple?) question is how to modify this result so it handles like a normal array, with values like:

 'code' => 'IH', 'name' => BTW 21%

Any clue?

Thx in advance!

karel
  • 53
  • 6
  • That's a lot of redundant strings. My first thought as a non-php guy would be a regex. Please demonstrate what you have actually tried so people with more experience can/will offer refinements. – Paul Hodges Nov 17 '17 at 21:04
  • Can you show the code that generates that output? Are you actually getting an array and iterating through that? – Patrick Q Nov 17 '17 at 21:06
  • Not entirely sure what you're after, but have you looked at the classic `json_encode() / json_decode()` to [convert objects to an assoc array](https://stackoverflow.com/a/10631793/3585500)? – ourmandave Nov 18 '17 at 02:39
  • Are you calling third party api? – Naim Malek Nov 18 '17 at 04:44
  • @NaimMalek: indeed. It's is a third party api, Twinfield – karel Nov 19 '17 at 12:44
  • Try this `$result = get_object_vars($YOUR_OBJECT));` @karel – Naim Malek Nov 20 '17 at 04:40

1 Answers1

0

I did following and it works. First changed object to regular array.

            foreach ($result as $array_item) {
            $item = (array) $array_item;
            $keys = array_keys($item);
            $key_first = $keys[0];
            $key_second = $keys[1]; 
            echo $item[$key_first];
            echo $item[$key_second];
            }
karel
  • 53
  • 6