Replacing while with foreach?



我正在替换这条线:

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

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

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

foreach构造有两种形式:( foreach $ foo作为$ bar(和(foreach $ array as $ key => value(。因此,逗号分隔的"值"或看来似乎不起作用,因为您显然具有多维数据结构(每个$键的一个以上$ value(。

您也不说($ Currencies->货币(是数组还是另一个对象。

如果$ Currencies->货币是 object

foreach ($currencies->currencies as $myObject) {
    echo "The key is: "   . $myObject->key; 
    echo "The value is: " . $myObject->value;
    echo "The title is: " . $myObject->title;
}

如果$ crentrencies->货币为 array

foreach ($currencies->currencies as $myArray) {
    echo "The key is: "   . $myArray['key']; 
    echo "The value is: " . $myArray['value'];
    echo "The title is: " . $myArrat['title'];
}

最新更新