1

Is it possible to set foreach function more than 2 condition. Example like below:

$stat_cps = $_POST['stat_cp'];
$acc_idss = $_POST['acc_ids'];

$string = 'NG';
foreach ($stat_cps as $url)
{
     if(strpos($string, $url) !== FALSE)
     {
          echo "One of the field is NG";
          return true;
     }
}

echo "All field is OK";

What I want is:
if $stat_cps or $acc_idss contains NG then echo "One of the field is NG";

On the above code, it's only working for $stat_cps

*stat_cps and acc_idss is from radio button form.

Anyone can give the suggestion?

9
  • 1
    array_merge to merge both your arrays into 1? Commented Feb 26, 2016 at 6:56
  • Any example @TommyBs ? Commented Feb 26, 2016 at 6:56
  • $newarray= array_merge($stat_cps, $acc_idss); now foreach on $newarray Commented Feb 26, 2016 at 6:59
  • what value come this varibales $stat_cps and $acc_idss ? Commented Feb 26, 2016 at 7:00
  • show sample values ? Commented Feb 26, 2016 at 7:08

4 Answers 4

2

"One-line" solution with array_merge, implode and strpos functions:

...
$hasNg = strpos("NG", implode(",", array_merge($stat_cps,$acc_idss)));
...

// check for 'NG' occurance
echo ($hasNg !== false)? "One of the field is NG" : "string 'NG' doesn't exists within passed data";
Sign up to request clarification or add additional context in comments.

1 Comment

nice concise solution, that's what I like about this site, you always see things you didn't think of it combining various features of a language
1

You can use array_merge to combine both your arrays:

http://php.net/manual/en/function.array-merge.php

$stat_cps = $_POST['stat_cp'];
$acc_idss = $_POST['acc_ids'];
$merged = array_merge($stat_cps,$acc_idss);
$string = 'NG';
foreach ($merged as $url)
{
     if(strpos($string, $url) !== FALSE)
     {
          echo "One of the field is NG";
          return true;
     }
}

This will remove duplicate string keys but it doesn't look like that should be an issue here if you just want one match

From you comment try doing

 $stat_cps = $_POST['stat_cp'];
$acc_idss = $_POST['acc_ids'];
$merged = array_merge($stat_cps,$acc_idss);
$match = false;
$string = 'NG';
foreach ($merged as $url)
{
     if(strpos($string, $url) !== FALSE)
     {
          $match =  true;
     }
}

if($match){
       echo "One of the field is NG";
}else{
      echo "Everything is OK";
}

5 Comments

Hi... when I put echo "All field is OK"; outside foreach it always give result OK even though one of the field contains NG.
If it's outside the foreach then it will always display regardless as it's not applied to that logic
From your comment if you just want a match (your return true is doing nothing) try using the second code example that sets a flag based on the existence (called $match)
How about if I have database query there? Actually what I want is when the 2 string (with multiple values from radio button) have NG status, then it will update the database table NG, but if All OK then OK
Then add your mysql query to the if($match){} statement where it echoes "One of the field is NG" I'm sorry and I don't want to seem rude but if you're struggling to write a MySQL query that's a separate question. I'm not going to write everything for you as you should make an attempt yourself otherwise it just feels like you're getting someone else to do your work.
0
$stat_cps = $_POST['stat_cp'];
$acc_idss = $_POST['acc_ids'];

$process=0;
$string = 'NG';
foreach ($stat_cps as $url)
{
     if(strpos($string, $url) !== FALSE)
     {
          $process=1;
     }
}
foreach ($acc_idss as $url2)
{
     if(strpos($string, $url2) !== FALSE)
     {
          $process=1;
     }
}
if($process==1){
  echo "One of the field is NG";
  return true;
}

6 Comments

@HiDayurie Dave : check this
Hi, actually in that condition I have database query to save the field. If using 2 foreach like your code I think I need to have 2 database query. Is it right?
no need to take 2 database query.U can use single query
Example, if NG then save the query to be NG, if OK then save the query to be OK.
u can also set condition in one query.No need to perform 2 queries.
|
0
#header  user in_array()
    $stat_cps = $_POST['stat_cp'];
    $acc_idss = $_POST['acc_ids'];

    $string = 'NG';
    foreach ($stat_cps as $url)
    {
         if(in_array($string, $url) !== FALSE)
         {
              echo "One of the field is NG";
              return true;
         }
    }

1 Comment

this doesn't solve the initial problem "... more than 2 condition"

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.