从php变量中删除值

时间:2014-06-13 12:39:59

标签: php mysql content-management-system

如何从$sku变量中删除一些值?它会从DB返回211016304565等。

代码:

$products = SProductVariantsQuery::create()->find()->toArray();

foreach ($products as $variant) {
$sku = $variant['Number'];
}

var_dump($ sku)返回:

string(4) "2250" string(4) "2251" string(3) "428" string(3) "427" string(4) "2800" string(4) "2804"

1 个答案:

答案 0 :(得分:1)

在执行foreach之前创建一个包含要排除的值的数组,然后检查该值是否在那里:

// Exclude prod. no 427 and 2800
$exclude = array('427', '2800');

foreach ($products as $variant) {
    // Only set $sku if the Number is not in the exclude array
    if (!in_array($variant['Number'], $exclude)) {
        $sku = $variant['Number'];
    }
}