簡體   English   中英

函數將變量中的逗號分隔值作為單個字符串

[英]Function taking Comma separated values in variable as a single string

IM 試圖過濾產品 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)

我該怎么解決!

為什么要將數組內爆為字符串然后傳遞它

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

然后使用作為

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM