简体   繁体   English

查找数组的下一个值

[英]Finding the next value of an array

Here I need to check whether the value comes after 1 is 1.1. 在这里,我需要检查值是否在1之后为1.1. Next value comes after 1.1 is 1.2. 下一个值在1.11.2. etc. 等等

The next value after a given value is takes by $C=$A[i]+'.'+$count; 给定值之后的下一个值为$C=$A[i]+'.'+$count; but when I print it prints values 1,2,3,4 etc. Is the way I have approached to the problem incorrect. 但是当我打印时,它会打印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

}
}
?>

You had a lot of syntax error, every variable in php must be prefixed with $. 您有很多语法错误,php中的每个变量都必须以$为前缀。 You missed that on the i-variable a couple of times. 您几次错过了i变量。 You also missed brackets in the for-loop. 您还错过了for循环中的括号。 And your $A variable was printed out because it was written before the PHP-tag. $ A变量被打印出来,因为它是在PHP标记之前编写的。

I cleaned it up and corrected those issues: 我清理并纠正了这些问题:

$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".
    }
}

I hope this is what you are looking for. 我希望这是您要寻找的。

<?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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM