user3526204 user3526204 - 4 months ago 8
PHP Question

in_array fails when using implode in if statement

This attamept is to create a search condition where eventually the output will be processed to show results based on increasing order of matches

"PHP implode function in if and foreach condition [closed]" is a different question !

Why is the following code failing ?

$terms = array("HELLO", "HI", "HOWDY");
$row = array("HELLO", "HI", "Hey");
$chkcond = "in_array('".implode("',$"."row".")"." && in_array"."('",$terms)."',$"."row)";

echo "$chkcond<br/><br/>";

if ($chkcond) {
echo "All Found in Array !<br>";}else{echo "Not Found !<br/>";
}


The echo result is

in_array('HELLO',$row) && in_array('HI',$row) && in_array('HOWDY',$row)


And the if condition outputs
"All Found in Array !"


When the
if
condition says that all three terms have to be in the row array to be "
All Found in Array
", then why is it returning
True
when "
Howdy
" doesn't exist in the row array ?

mmm mmm
Answer

juste use array_diff http://php.net/array_diff

<?php
$terms=array("HELLO","HI","HOWDY");
$row=array("HELLO","HI","Hey");

$diff = array_diff($terms, $row);

if (0 === count($diff)){echo "All terms Found in Array row !<br>";}else{echo "Not all terms Found in Array row !<br>";}


$terms=array("HELLO","HI","HOWDY");
$row=array("HELLO","HI","Hey","HOWDY");

$diff = array_diff($terms, $row);

if (0 === count($diff)){echo "All terms Found in Array row !<br>";}else{echo "Not all terms Found in Array row !<br>";}
Comments