【问题标题】:Function taking Comma separated values in variable as a single string将变量中的逗号分隔值作为单个字符串的函数
【发布时间】:2014-04-14 09:48:49
【问题描述】:

我正在尝试在产品 magento ce 1.7 中过滤某些产品。我正在从多维数组中获取我想要过滤的值

foreach ($artist_productIds as $artist_productID){
    $artist_product_id[] = $artist_productID['mageproductid'];
}
$artist_prodIdString = implode(',',$artist_product_id); 

并将其传递给 magento 查询

$categoryproducts = Mage::getModel('catalog/category')->load($currentArtCat)
 ->getProductCollection()
 ->addAttributeToSelect('*') // add all attributes - optional
 ->addFieldToFilter('status', array('neq' => 2))
 ->addAttributeToFilter('entity_id', array('nin' => array($artist_prodIdString)));

在调试时我发现它传递的值是

array('47,48,49,112,113,114,115,116')

它应该像这样通过

array(47,48,49,112,113,114,115,116)

我该怎么解决!

【问题讨论】:

标签: php mysql sql magento


【解决方案1】:

为什么要将数组内爆到字符串然后传递它

$artist_prodIdString = implode(',',$artist_product_id); 

然后使用 as

array('nin' => array($artist_prodIdString));

你可以直接将数组传递为

 array('nin' => $artist_product_id);

你在 foreach 中生成的。

如果您的 $artist_product_id 不是正确的数组 然后在

$artist_prodIdString = implode(',',$artist_product_id); 

使用explode()将其变成一个数组并将其传递给函数。

你正在尝试一些事情

$str = '47,48,49,112,113,114,115,116' ;
$array = array($str);
print_r($array);
output Array
(
    [0] => 47,48,49,112,113,114,115,116
)

您的情况也是如此,所有数字都用于数组键 0

【讨论】:

    猜你喜欢
    • 2017-02-02
    • 2016-10-19
    • 2016-02-08
    • 2013-07-14
    • 2013-06-11
    • 1970-01-01
    • 2012-08-12
    • 2016-12-22
    • 2020-07-10
    相关资源
    最近更新 更多