每个结果的PHP不包括数组中的值

时间:2021-06-27 13:39:58

I queried a table into an array but I would like to exclude certain values from one of the columns as to spits out each row. For example.

我查询了一个表到一个数组,但我想从其中一个列中排除某些值,以吐出每一行。例如。

foreach($results as $row)
{
         if($row['sku'] != "sku-1"){
             echo $row['sku']." ";
         }
}

So lets say the table has 4 rows with a value of sku-1, sku-2, sku-3 and sku-4. The above Foreach code would echo out "sku-2 sku-3 sku-4". Is there a way I can make an array of what values I would want to exclude? Like I'd have an array called $skuarray = "sku-2, sku-4" and instead of

因此,假设该表有4行,其值为sku-1,sku-2,sku-3和sku-4。上面的Foreach代码将回显“sku-2 sku-3 sku-4”。有没有办法可以制作一个我想要排除的值的数组?就像我有一个名为$ skuarray =“sku-2,sku-4”的数组而不是

if($row['sku'] != "sku-2" || $row['sku'] != "sku-4"){

have that $skuarray in there where it'll echo "sku-1, sku-3"? Thanks!

有那个$ skuarray在那里它会回应“sku-1,sku-3”?谢谢!

EDIT

编辑

I could somehow exclude it when I query it. I'm querying it from table SKUTABLE and the column is SKU. The problem is I need to exclude unique values from column SKU so I thought if there was an easy way to just throw in all the ones I want to exclude into an array that'd be great.

当我查询它时,我可以以某种方式排除它。我从表SKUTABLE查询它,列是SKU。问题是我需要从列SKU中排除唯一值,所以我想如果有一种简单的方法可以将我要排除的所有值排除在一个非常棒的数组中。

4 个解决方案

#1


0  

Use in_array:

使用in_array:

$skuarray = array("sku-2", "sku-4");

foreach($results as $row)
{
         if(!in_array($row['sku'], $skuarray)){
             echo $row['sku']." ";
         }
}

#2


1  

You could use the array_diff(array1, array2, [...arrayN]) function, which takes at least two arrays, and returns an array of only those values of array1 which are not in any of the subsequent arrays. Example:

您可以使用array_diff(array1,array2,[... arrayN])函数,该函数至少需要两个数组,并返回一个仅包含array1的值的数组,这些值不在任何后续数组中。例:

$input = array(0=>'sku-1',1=>'sku-2',2=>'sku-3',3=>'sku-4');
$exclude = array('sku-1','sku-3','sku-4');
$result = array_diff($input, $exclude);
print_r($result);

Will print

会打印

array(1=>'sku-2');

#3


0  

foreach($results as $row)
{
         if(!array_key_exists($row['sku'], $skuarray)) {
             echo $row['sku']." ";
         }
}

#4


0  

You can use array_filter in combination with in_array.

您可以将array_filter与in_array结合使用。

$filtered_results = array_filter($results, function ($row) {
    return in_array($row["sku"], $skuarray) === false;
});

#1


0  

Use in_array:

使用in_array:

$skuarray = array("sku-2", "sku-4");

foreach($results as $row)
{
         if(!in_array($row['sku'], $skuarray)){
             echo $row['sku']." ";
         }
}

#2


1  

You could use the array_diff(array1, array2, [...arrayN]) function, which takes at least two arrays, and returns an array of only those values of array1 which are not in any of the subsequent arrays. Example:

您可以使用array_diff(array1,array2,[... arrayN])函数,该函数至少需要两个数组,并返回一个仅包含array1的值的数组,这些值不在任何后续数组中。例:

$input = array(0=>'sku-1',1=>'sku-2',2=>'sku-3',3=>'sku-4');
$exclude = array('sku-1','sku-3','sku-4');
$result = array_diff($input, $exclude);
print_r($result);

Will print

会打印

array(1=>'sku-2');

#3


0  

foreach($results as $row)
{
         if(!array_key_exists($row['sku'], $skuarray)) {
             echo $row['sku']." ";
         }
}

#4


0  

You can use array_filter in combination with in_array.

您可以将array_filter与in_array结合使用。

$filtered_results = array_filter($results, function ($row) {
    return in_array($row["sku"], $skuarray) === false;
});