【发布时间】:2011-12-23 19:29:35
【问题描述】:
由于某种原因,$post 总是
由于某种原因,即使我在下一个循环中添加了这样的元素array_push($groups, $tempDon);,我仍然继续返回 -1
$donations = $this->getInstitutionDonations($post->ID);
$groups=array();
foreach( $donations as $don ) : setup_postdata($don);
$pos = $this->indexOf($don, $groups);
print_r($pos);
if($pos < 0)
{
$tempDom = $don;
$tempDon->count = 1;
array_push($groups, $tempDon);
}
else
{
$tempDom = $groups[$pos];
$tempDon->count++;
array_splice($tempDon);
array_push($groups, $tempDon);
echo '<br><br><br>ahhhhhhhhhh<br><br>';
}
endforeach;
protected function indexOf($needle, $haystack) { // conversion of JavaScripts most awesome
for ($i=0;$i<count($haystack);$i++) { // indexOf function. Searches an array for
if ($haystack[$i] == $needle) { // a value and returns the index of the *first*
return $i; // occurance
}
}
return -1;
}
【问题讨论】:
-
好吧,也许是因为你从来没有改变过
$pos的值? -
请不要混合使用 if / foreach 等的标准和替代语法。您的代码的可读性会增加。旁边
indexOf的定义未知(需要加代码),$don是什么? -
-1??好吧,我应该更改
$pos = $this->indexOf($don, $groups);所以下次循环时groups将有一个元素,我强制元素相同......仍然不明白为什么 -1 这不公平 -
$don是在 foreach 上指定的数组$donations的当前元素