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!

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. Commented Nov 17, 2017 at 21:04
  • Can you show the code that generates that output? Are you actually getting an array and iterating through that? Commented Nov 17, 2017 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? Commented Nov 18, 2017 at 2:39
  • Are you calling third party api? Commented Nov 18, 2017 at 4:44
  • @NaimMalek: indeed. It's is a third party api, Twinfield Commented Nov 19, 2017 at 12:44

1 Answer 1

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];
            }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.