dpspn60064 2014-11-27 22:58
浏览 342

获得foreach循环结果的最大值

I am trying to get the result with the largest number in a substring from a foreach loop. The code below returns 2 strings which only differ as one contains a substring of "220px" the other a substring of "24px". I'd like to return as a variable "220" or the highest number in any number of substrings. I used "simple_html_dom.php" from http://simplehtmldom.sourceforge.net/. Any assistance is much appreciated.

<html>
<body>
<h2>Search</h2>
<form method="post">
Search: <input type="text" name="q" value="google"/>
<input type="submit" value="Submit">
</form>

<?php

include 'simple_html_dom.php';  

if (isset($_POST['q'])) {
$search = $_POST['q']; 
$html = file_get_html("http://en.wikipedia.org/wiki/$search");
?>
<h2>Search results for '<?php echo $search; ?>'</h2>
<ol>

<?php

foreach ($html->find('img') as $element): ?>


<?php $photo = $element->src;
$logo = 'Logo';

if(strpos($photo, $logo)) 
{

if (preg_match_all('/[0-9]+px/', $photo, $result)) {
echo '<br/>';

$rp = trim($result[0][0],"px") .'<br/>';
echo $photo;

} else {
echo "Not found";
}
} 

?>              

<?php endforeach;?>
</ol>
<?php 
}

?>
</body>
</html>
  • 写回答

1条回答 默认 最新

  • doumang20060820 2014-11-27 23:05
    关注

    Heres some code for a foreach that your trying to obtain.

    $highest = 0;
    foreach($array as $string) {
       $number = intval($string);
       if($number > $highest) {
           $highest = $number
       }
    }
    
    // highest is now the highest number gotten from the strings of the foreach loop
    
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)