簡體   English   中英

forEach跳過循環中的最后一個元素

[英]forEach skips last element in loop

我正在遍歷一個countries對象。 但是,當我引用該對象時,我注意到倒數第二個元素被重復,而最后一個元素被跳過。 烏拉圭出現了兩次,而美國根本沒有出現。

foreach($countries as &$country){
    $country->keywords = array_merge(array($country->name),$country->synonyms);
    var_dump($country->key);
}

var_dump('-----------------------------------------');

foreach($countries as $country){
    var_dump($country->key);
}

exit;

和我的輸出。 一直滾動到底部,以查看uruguay現在出現了兩次:

string(7) "algeria"
string(9) "argentina"
string(9) "australia"
string(7) "belgium"
string(6) "bosnia"
string(8) "cameroon"
string(5) "chile"
string(8) "columbia"
string(9) "costarica"
string(5) "ivory"
string(7) "croatia"
string(8) "ecquador"
string(7) "england"
string(6) "france"
string(7) "germany"
string(5) "ghana"
string(6) "greece"
string(8) "honduras"
string(4) "iran"
string(5) "italy"
string(5) "japan"
string(6) "mexico"
string(11) "netherlands"
string(7) "nigeria"
string(8) "portugal"
string(6) "russia"
string(5) "korea"
string(5) "spain"
string(11) "switzerland"
string(7) "uruguay"
string(7) "america"
string(41) "-----------------------------------------"
string(7) "algeria"
string(9) "argentina"
string(9) "australia"
string(7) "belgium"
string(6) "bosnia"
string(8) "cameroon"
string(5) "chile"
string(8) "columbia"
string(9) "costarica"
string(5) "ivory"
string(7) "croatia"
string(8) "ecquador"
string(7) "england"
string(6) "france"
string(7) "germany"
string(5) "ghana"
string(6) "greece"
string(8) "honduras"
string(4) "iran"
string(5) "italy"
string(5) "japan"
string(6) "mexico"
string(11) "netherlands"
string(7) "nigeria"
string(8) "portugal"
string(6) "russia"
string(5) "korea"
string(5) "spain"
string(11) "switzerland"
string(7) "uruguay"
string(7) "uruguay"
foreach($countries as &$country){
    $country->keywords = array_merge(array($country->name),$country->synonyms);
    var_dump($country->key);
}
unset($country);

var_dump('-----------------------------------------');

foreach($countries as $country){
    var_dump($country->key);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM