-2

i want to compare two values in one php array but the code stops when i compare even when the conditions is true i want to know how can i compare the two values here is my code :

$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);
Richard Parnaby-King
  • 14,703
  • 11
  • 69
  • 129
M.RIRI
  • 3
  • 6

2 Answers2

2

This is probably because of the line

$j+1;

your both variables ($i and $j) are not being updated in the while loop, causing an infinite loop. ( checking the same values all the time, if the condition is true an infinite loop, else the code will never enter the loop and exit. )

change $j+1; with either $j++; or $j = $j + 1;

Moreover as @apomene shows,

if your array can have multiple types,

The !== operator checks both the type and the equality. If your array has same types ( for e.g int ) this would not create a problem tho. With the same types !== and != are the same thing practically. Else, it (!==) also checks for type equality. To elaborate,

$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)

Further reading available in this question.

Lastly, you seem to have a confusion between assignment and comparison of a variable. ( $i = $j vs $i == $j )

Check the php manual for assignment vs comparison of variables.

epipav
  • 339
  • 2
  • 14
  • thanks for your answer.i correct my mistakes but its not giving me what am looking for : what i want to do is give a color to each element of my erray but : evry element have it uniquecolor and if this element come back in the erray it will have the same color – M.RIRI Oct 12 '17 at 09:46
  • you are welcome, if it helped you & solved your problem, please mark it as answered. – epipav Oct 12 '17 at 09:47
  • the array am using look like this : $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" } but i juste print $array[$i] in the while loop and it gives me this : kssnnn TTTTTarray am confused why it dosnt give me the values ? – M.RIRI Oct 12 '17 at 09:56
  • copy the piece of code when you try to print the values of array. I can not help you not knowing the code you are working with. Do not forget to enclose your codes in codeblocks using apostrophe. such as `code` – epipav Oct 12 '17 at 10:22
  • ` $newarray = ['test coupe','skynet', 'test coupe','skynet', 'test coupe', 'Sans-Titre']; $couleur=array(); $i=0;$cmpt=0; foreach($newarray as $newarray1){ $j=0; while ($newarray1[$i]!=$newarray1[$j]){ echo $newarray1[$i];// when i print here it gaves me kssnnn TTTTT $j++; } if ($i==$j){ $couleur[]=$Tcouleur[$cmpt]; $cmpt+1; //var_dump($cmpt); }else{ $couleur[]=$Tcouleur[$j]; } $i++; }` – M.RIRI Oct 12 '17 at 12:09
  • hi again, you should be doing `echo $newarray1;` instead of `$newarray1[$i]` because you are looping throught items of `$newarray` already using foreach. In another words, `foreach($newarray as $newarray1)` --> each iteration contains the next element of `$newarray` inside `$newarray1` – epipav Oct 12 '17 at 12:22
1

In your while loop, does $j+1 should not be $j++ or $j = $j + 1 ?

I know it's not the problem your asking...but same for your $i+1 at the end and your $cmpt

Now I think you want this :

$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);
SpOOnisBacK
  • 119
  • 7
  • Can you give us `$newarray` and `$TCouleur` example ? – SpOOnisBacK Oct 12 '17 at 09:54
  • $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" [4]=> string(7) "#BA0CF5" [5]=> string(7) "#BA0CF5" } but it suppose to be unique color for each element and when the element apear again in the array it hase to give him the same color – M.RIRI Oct 12 '17 at 10:00
  • in my response, `$couleurs` should have enough colors to fill the different `$values` you need – SpOOnisBacK Oct 12 '17 at 10:02
  • the result i want in your exemple is $couleurPourValeur=['rouge,vert,bleu,jaune,vert,jaune,rose] . when the same value come back i give it the same color as befor i hope is clear – M.RIRI Oct 12 '17 at 12:03