[英]Finding the next value of an array
在这里,我需要检查值是否在1
之后为1.1.
下一个值在1.1
为1.2.
等等
给定值之后的下一个值为$C=$A[i]+'.'+$count;
但是当我打印时,它会打印1,2,3,4
等值。我解决该问题的方法是否正确。
<?
$A=array(1, 1.1, 1.2, 1.3, 1.4);
$count=0;
for($i=0;$i<sizeof($A);$i++){
$count++;
$B=$A[$count];
$C=$A[i]+'.'+$count;
if($B==$C){
//a code goes here
}
}
?>
您有很多语法错误,php中的每个变量都必须以$为前缀。 您几次错过了i变量。 您还错过了for循环中的括号。 $ A变量被打印出来,因为它是在PHP标记之前编写的。
我清理并纠正了这些问题:
$A = array(1, 1.1, 1.2, 1.3, 1.4);
$count = 0;
for($i = 0; $i < sizeof($A) - 1; $i++){
$count++;
$B = $A[$count];
$C = $A[$i] + 0.1;
if($B == $C){
// This will be executed every time the next value is "current + 0.1".
}
}
我希望这是您要寻找的。
<?php
$a=array(1, 1.1, 1.2, 1.3, 1.4);
$count=0;
foreach($a as $v)
{
$c = "1".'.'.$count;
if($c==$v)
{
echo "Match Found";
}
$count++;
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.