function regex_scrape($regex,$results_page){
preg_match_all($regex,$results_page,$match);
return $match;
}
$continue = TRUE;
$prices = array();
$url = "";
while ($continue == TRUE) {
$results_page = curl($url); // Downloading the results page using our curl() funtion
$prices2 = regex_scrape('/<span class=\"h3 price-amount\">(.*)span>/',$results_page);
$prices = array_merge($prices,$prices2[0]);
Array看起来像这样,我不能使用array_sum或找到最低的数组值(min)
Array ( [0] => 45 [1] => 80 [2] => 60 [3] => 40 [4] => 37 [5] => 69 [6] => 34 [7] => 79 [8] => 46 [9] => 91 [10] => 269 [11] => 59 [12] => 60 [13] => 79 [14] => 35 [15] => 67 [16] => 85 [17] => 45 )
echo array_sum($prices);
返回0为什么?
答案 0 :(得分:3)
您的示例数组没有连续索引(可能来自array_merge
)。您是否尝试过先array_values
?
echo array_sum(array_values($prices));
答案 1 :(得分:1)
如果你发布的数组是你的代码正在处理的实际数组,我没有看到任何问题。
$prices = array ( '0' => 45 ,'1' => 80 ,'2' => 60 ,'3' => 40 ,'4' => 37 ,'5' => 69 ,'6' => 34 ,'7' => 79 ,'8' => 46 ,'9' => 91 ,'10' => 269 ,'11' => 59 ,'12' => 60 ,'13' => 79 ,'14' => 35 ,'15' => 67 ,'16' => 85 ,'17' => 45 );
var_dump( $prices );
echo array_sum( $prices );
以上代码给我的总和为1280
。
此外,数组索引不必是连续的:
$prices = array ( '100' => 45 ,'90' => 80 ,'80' => 60 ,'70' => 40 ,'4' => 37 ,'5' => 69 ,'6' => 34 ,'7' => 79 ,'8' => 46 ,'9' => 91 ,'10' => 269 ,'11' => 59 ,'12' => 60 ,'13' => 79 ,'14' => 35 ,'15' => 67 ,'16' => 85 ,'17' => 45 );
var_dump( $prices );
echo array_sum( $prices );
上面的代码也给了我一笔钱1280
。
我发现您在下面的评论中发布的数组没有任何问题:
$prices = array( 0 => "45" ,1 => "40" ,2 => "85" ,3 => "46" ,4 => "69" ,5 => "34" ,6=> "60" ,7=> "91" ,8=> "86" ,9=> "48" ,10=> "70" ,11=> "80" ,12=> "42" ,13=> "67" ,14=> "108" ,15=> "37" ,16=> "45" ,17=> "95" );
var_dump( $prices );
echo array_sum( $prices );
这总和为1148
。