用foreach替换?

时间:2018-02-12 18:07:23

标签: php

这条线我要替换:

reset($currencies->currencies); 
while (list($key, $value, $title) = each($currencies->currencies)) { 

我认为这是正确的?或者我错了吗?

foreach($currencies->currencies as $key => $value, $title) { 

1 个答案:

答案 0 :(得分:0)

foreach结构有两种形式:(foreach $ foo as $ bar)和(foreach $ array as $ key => value)。因此,以逗号分隔的"值"似乎没有工作,或者看起来如此,因为你显然有一个多维数据结构(每个$ key的值超过一个$)。

您也不会说($ currency-> currency)是数组还是其他对象。

如果$ currency->币种是对象

foreach ($currencies->currencies as $myObject) {

    echo "The key is: "   . $myObject->key; 
    echo "The value is: " . $myObject->value;
    echo "The title is: " . $myObject->title;
}

如果$ currency->货币是数组

foreach ($currencies->currencies as $myArray) {

    echo "The key is: "   . $myArray['key']; 
    echo "The value is: " . $myArray['value'];
    echo "The title is: " . $myArrat['title'];
}