Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP检查数组[$i]=的值!数组[$j]_Php_Arrays - Fatal编程技术网

PHP检查数组[$i]=的值!数组[$j]

PHP检查数组[$i]=的值!数组[$j],php,arrays,Php,Arrays,我想比较一个php数组中的两个值,但当我比较时,即使条件为真,代码也会停止。我想知道如何比较这两个值这是我的代码: $i=0;$cmpt=0; foreach($newarray as $newarray1){ $j=0; while ($newarray1[$i]!==$newarray1[$j]){ // the iteration dont get in here even when the condition is true $j+1; v

我想比较一个php数组中的两个值,但当我比较时,即使条件为真,代码也会停止。我想知道如何比较这两个值这是我的代码:

$i=0;$cmpt=0;
foreach($newarray as $newarray1){
    $j=0;
    while ($newarray1[$i]!==$newarray1[$j]){ // the iteration dont get in here even when the condition is true
        $j+1;
        var_dump($j);
    }
    if ($i=$j){
        $couleur[]=$Tcouleur[$cmpt];
        $cmpt+1;
    }else{
        $couleur[]=$Tcouleur[$j]; 
    }
    $i+1;
}
var_dump($couleur);

在while循环中,
$j+1
是否不应该是
$j++
$j=$j+1

我知道这不是你问的问题…但最后你的
$I+1
和你的
$cmpt

现在我想你想要这个:

$values = ['abc','def', 'hij','klm', 'def', 'klm','nop'];
$couleurs = ['rouge','vert','bleu','jaune','rose'];
$couleurPourValeur = [];
$increment = 0;
foreach($values as $value){
    if(!isset($couleurPourValeur[$value])){
        $couleurPourValeur[$value] = $couleurs[$increment];
        $increment++;
    }
}
print_r($couleurPourValeur);

这可能是因为线路的原因

$j+1;
您的两个变量($i和$j)没有在while循环中更新,从而导致无限循环。(始终检查相同的值,如果条件为真,则为无限循环,否则代码将永远不会进入循环并退出。)

更改
$j+1$j++
$j=$j+1

此外,正如@apomene所示

如果数组可以有多种类型

==运算符检查类型和相等性。如果您的数组具有相同的类型(例如int),则不会产生问题。使用相同类型的
==
=实际上是一样的。否则,它(
!==
)还会检查类型是否相等。详细说明,

$a = 1;
$b = '1';
$c = 2;
$d = 1;

$a == $b  //  TRUE   ( different type, equal after conversion - char <-> int)
$a === $b //  FALSE( different types - int vs char)
$a == $c  //  FALSE( same type not equal)
$a === $d //  TRUE ( same type and equal)
$a=1;
$b='1';
$c=2;
$d=1;
$a==$b//TRUE(不同类型,转换后相等-char int)
$a==$b//FALSE(不同类型-int与char)
$a==$c//FALSE(同一类型不相等)
$a==$d//TRUE(相同类型且相等)
有关的进一步阅读

最后,您似乎混淆了变量的赋值和比较。(
$i=$j
vs
$i==$j


检查php手册中变量的vs

$j+1
是一个问题。如果($i=$j),你能详细说明这个问题吗
-你应该仔细阅读=vs.==和===谢谢你的回答。我纠正了我的错误,但没有给我我想要的:我想做的是给我的勘误表的每个元素一个颜色,但是:evry元素有它唯一的颜色,如果这个元素在勘误表中返回,它将有相同的颜色欢迎你,如果它帮助您解决了问题,请将其标记为已回答。我使用的数组如下:$newarray{[0]=>string(10)“test coupe”[1]=>string(6)“skynet”[2]=>string(10)“test coupe”[3]=>string(6)“skynet”[4]=>string(10)“test coupe”[5]=>string(10)“Sans Titre”}但我只是打印了$array[$i]在while循环中,它给了我这个:kssnnn ttttt数组我不明白为什么它没有给我值?当你试图打印数组的值时,复制这段代码。我不能帮助你不知道你正在使用的代码。不要忘记使用撇号将代码包含在代码块中。例如,
code
“$newarray=[‘测试轿厢’、‘天网’、‘测试轿厢’、‘天网’、‘测试轿厢’、‘无标题’]$couleur=array()$i=0$cmpt=0;foreach($newarray as$newarray1){$j=0;while($newarray1[$i]!=$newarray1[$j]){echo$newarray1[$i];//当我在这里打印时,它给了我kssnnn ttttttt$j++}如果($i==$j){$couleur[$cmpt];$cmpt+1;//变量转储($cmpt){$couleur[]=$t规则[$j];}$i++}你能给我们举个例子吗?$newarray{[0]=>string(10)“test coupe”[1]=>string(6)“skynet”[2]=>string(10)“test coupe”[3]=>string(6)“skynet”[4]=>string(10)“test coupe”[5]=>string(10)“Sans Titre”}--$Tcouleur{[0]=>string(7)”#BA0CF5“[1]=>string(7)”#BA0CF5“[2]=>string(7)”#BA0CF5“[3]=>string(7)”#BA0CF5”=>string(7)”#BA0CF5“[5]=>string(7)”#BA0CF5”=>但是假设每个元素的颜色都是唯一的,当元素再次在数组中移动时,它会在我的响应中给他相同的颜色,
$couleurs
应该有足够的颜色来填充不同的
$value
您需要我在您的示例中想要的结果是$couleurporvaleur=['rouge,vert,bleu,jaune,vert,jaune,rose].当相同的值返回时,我给它与以前相同的颜色,希望它是清晰的