2

I would like to know how I could do the following differently because I feel the way I'm doing it is wrong and would make a developer cringe.

$prodcat is an array with id's and I want to make a warning for when certain id's are in the array. This is what I do now and it does do the job but I'm sure there's a more efficient way of coding it.

$valuecheck = false;

foreach ($prodcat as $daafid) {
    if (($daafid == '96') || ($daafid == '97') || ($daafid == '99') || ($daafid == '122') || ($daafid == '123') || ($daafid == '124') || ($daafid == '125') || ($daafid == '126') || ($daafid == '127') || ($daafid == '128') || ($daafid == '129') || ($daafid == '130') || ($daafid == '132') || ($daafid == '133')) {
        $valuecheck = true;
    }
}
if ($valuecheck == true) {
    echo $text_true;
}

Thanks a lot in advance.

3
  • put the values you want to check for in an array themselves, loop through that array and use the in_array function to check whether each value is contained within the products array or not. There might even more a less verbose way than that. Check the PHP docs there are all sorts of funky functions for working with arrays. Commented Aug 30, 2017 at 14:49
  • Do you want the warning (echo $text_true) shown once, or every time it encounters the condition? Commented Aug 30, 2017 at 14:56
  • Who down-voted my post? Reason will be helpful for me too... Commented Aug 30, 2017 at 15:30

5 Answers 5

1

Try array_intersect. It will also work if the IDs you are checking for are strings as your code seems to be indicating. The array_intersect considers two elements equal if and only if (string) $elem1 === (string) $elem2. In another words: when the string representation is the same.

$ids = Array(96, 97, 99, 122, ...);
if(array_intersect($ids, $prodcat));
    echo $text_true;
}
Sign up to request clarification or add additional context in comments.

Comments

1

I've given 2 examples depending on whether you want to output the message every time it encounters a category ID in the array, or just the message shown once if the condition occurs at all.

// Array of category ID's to check against.
$daafids_array = [96,97,99,122,123,124,125,126,127,128,129,130,131,132,133];

// Initialise an empty array to hold the warning message if it occurs.
$warning = [];

// Loop through each $prodcat
foreach ($prodcat as $daafid) {
    // Check if the ID is within the array
    if (in_array($daafid, $daafids_array)) {
        // Build up the array with the message if it occurs.
        $warning[$daafid] = $text_true;
    }
}

// Show warnings and the corresponding category ID every time it's happened.
if (!empty($warning)) {
    foreach ($warning as $key => $value) {
        echo $value . ' - for category ID ' . $key . '<br>';
    }
}

// Just show 1 warning once if it *ever* encounters the condition
if (!empty($warning)) {
    echo $text_true;
}

If $warning remains empty then nothing will be echo'd because there's nothing to display (the ID's in $prodcat were not found in $daafids_array).

Comments

1

I would just make a whitelist for the ids and check them with in_array()

you dont need to check with if, because in_array always returns true or false.

and don't forget to use === for $valuecheck === true

$valuecheck     = false;
$whiteListedIds = [96,97,99,122,123,124,125,126,127,128,129,130,131,132,133];

foreach ($prodcat as $daafid) {
   $valuecheck = in_array($daafid, $whiteListedIds);
}

if ($valuecheck === true) {
    echo $text_true;
}

Comments

0

You can do it something like this.

Solution 1: Here we are using array_intersect for checking common values between two arrays.

<?php

ini_set('display_errors', 1);
$valuecheck = false;
$daafids=array(96,97,99,122,123,124,125,126,127,128,129,130,131,132,133);
if(count(array_intersect($prodcat, $daafids))>0)
{
    $valuecheck=true;
}

Here we are using in_array to search that particular element in the array.

Solution 2:

<?php

ini_set('display_errors', 1);
$valuecheck = false;
$daafids=array(96,97,99,122,123,124,125,126,127,128,129,130,131,132,133);
foreach ($prodcat as $daafid) {
    if(in_array($daafid,$daafids)){
       $valuecheck=true;
       break;
    }
}
if ($valuecheck == true) {
    echo $text_true;
}

3 Comments

Thanks a lot @Sahil Gulati this did the trick as well. Thanks at everyone for all your answers btw!
@DavidWeermeijer glad to help you... :)
Please tell the reason of downvoting
0

In Array function probably would work better. Reference: https://www.w3schools.com/php/func_array_in_array.asp

2 Comments

Read the question carefully. This isn't what is being asked about.
My suggestion was like Sahil Gulati's 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.