Codeigniter for-each循环,带有API结果

时间:2017-11-17 20:50:39

标签: php arrays api codeigniter

我的API调用在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%
)

我的(简单?)问题是如何修改此结果,使其像正常数组一样处理,其值如下:

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

有任何线索吗?

提前谢谢!

1 个答案:

答案 0 :(得分:0)

我做了以下工作。首先将对象更改为常规数组。

            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];
            }