1

I'm having trouble with output in for each function in PHP (actually don't know how to set the code to do what I need). I'd like to output some text if every item in foreach is equal to some value. If I put

foreach($items as $item) {
    if($item == 0){ echo "true"; }
}

I will get true for every item and I need to output true only if all items are equal to some value.

Thanks!

5
  • can you give a simple source of $items? Commented Apr 24, 2014 at 13:42
  • 1
    Are you trying to check if all the elements in the array are same? Then just do: if (count(array_unique($items)) === 1) { ... }. Commented Apr 24, 2014 at 13:48
  • @AmalMurali i guess that's the easiest way, but if he had issues with the original code we should offer some more basic answers, developing his original code Commented Apr 24, 2014 at 13:50
  • @CatalinDeaconescu: It's perfectly fine as a comment. And I don't agree with that - if the OP's current approach is bad, say so, and show them the alternatives. Modifying the existing code to "make it work" is not going to be helpful. Commented Apr 24, 2014 at 13:53
  • 1
    @AmalMurali For me , programming is how you learn to think , improve your thinking process and logic. There is always easier way to solve problems , but if you Truly understand it, you can go further. We can point him to improve his post. But the easiest way should be added the last. To make sure the OP learned something from us , not copy and paste from us. Commented Apr 24, 2014 at 14:14

7 Answers 7

2

This is most likely due to PHP type juggling your values. Your values are probably not numeric so when you do a loose comparison (==) PHP converts them to integers. Strings that do not start with digits will become zero and your statement will be true.

To fix this use the === comparison operator. This will compare value and type. So unless a value is the integer zero it will be false.

if($item === 0){ echo "true"; }

If you are trying to see if all items are equal to some value this code will do this for you:

$equals = 0;
$filtered = array_filter($items, function ($var) use ($equals) {
    return $var === $equals;
});
if (count(count($items) === count($filtered)) {
    echo "true";
}
Sign up to request clarification or add additional context in comments.

3 Comments

this doesn't help with the requirement that all the items must be equal.
@John this is not what was asked, seems like people just upvote looking at reputation
That seems to be true. I updated my answer to account for what they are looking for.
1

This peice of code work for most type of variables. See how it works in inline comment.

 $count=0; // variable to count the matched words
 foreach ($items as $item)
   {
     if($item == $somevalue)
       {
         $count++; // if any item match, count is plus by 1
       }
   }
 if($count == count($items))
   {
     echo "true"; // if numbers of matched words are equal to the number of items
   }
 else
  {
    echo "false";
  }

Hope it works , And sorry for any mistake

2 Comments

Thank you, this answer is the most simple for me and works great.
@Moirae Thank you for using PoomrokcSoft service... Xb
0
$ok = true;
foreach ($items as $item) 
{
    if ($item != 0)
    { 
        $ok = false;
    }
}
if ( $ok == true)
{
    echo 'true';
}

Comments

0
$bool = 0;
foreach($items as $item) {
    if($item == $unwantedValue)
    { $bool=1; break; }
}

if($bool==0)
echo 'true';

Comments

0
$equals=true;

foreach($items as $item) {
    if($item!=0)
    {
        $equals=false;
        break;
    }
}
if($equals) {
    echo 'true';
}

Comments

0

if you want to check the values is same with some value in variabel and print it use this

<?php 
$target_check = 7;

$items = array(1, 4, 7, 10, 11);

foreach ($items as $key => $value) {
    if ($value == 7) echo "the value you want is exist in index array of " . $key . '. <br> you can print this value use <br><br> echo $items[' . $key . '];';
}


?>

but if you just want to check the value is exist in array you can use in_array function.

<?php

$target_check = 2;

if (in_array($target_check, $items)) echo "value " . $target_check . 'found in $items';
else echo 'sorry... ' . $target_check . ' is not a part of of $items.';
?>

3 Comments

What!? He only want to print "true" if all values in array are the same and is equal to some value. It doesn't have anything to do with in_array. Sorry if this comment hurt your feeling , but it's not meeting OP's requirements.
thanks. but the mission is [I'd like to output some text if every item in foreach is equal to some value.]. so it just not to print true.
This and This it's still saying it exists even not every itm is equal to $target_Check. Please fix it
-2
<?
$items=array(0,1,2,0,3,4);
foreach($items as $item) {
    if($item == 0){ echo "true"; }
}
?>

your code work! check the source of the $items array

1 Comment

please check the question properly before testing the code and posting an answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.