David Weermeijer
David Weermeijer

Reputation: 85

PHP Checking multiple values in an array - the right way?

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.

Upvotes: 2

Views: 1711

Answers (5)

Vural
Vural

Reputation: 8748

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;
}

Upvotes: 1

Sahil Gulati
Sahil Gulati

Reputation: 15141

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;
}

Upvotes: 0

Andy
Andy

Reputation: 5395

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

Upvotes: 1

Jarek.D
Jarek.D

Reputation: 1294

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;
}

Upvotes: 1

ByWaleed
ByWaleed

Reputation: 402

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

Upvotes: 0

Related Questions