2015-02-06 47 views
0

我有這樣PHP獲得該數組中的值作爲類似格式

Array 
(
    [0] => T-shirts 
    [1] => Evening Dresses 
    [2] => Dresses 
    [3] => Clothes 
) 

在這裏,我想要得到的數組的值,並添加「」每個值,並增加一個陣列,每個值 後因此,衣被合計應該終於來了這樣

IN("T-shirts","Evening Dresses","Dresses","Clothes") 

所以爲了這個,我做了我這樣的代碼

$category_pool = ''; 
foreach($categoryArray as $categoryArr) 
    $category_pool .= $categoryArr.','; 
    $category_pool = ((strpos($category_pool, ',') === false) ? (' = '.$category_pool.' ') : (' IN ("'.rtrim($category_pool, ',').'") ')); 

,但在這裏它是越來越值一樣

IN ("T-shirts,Evening Dresses,Dresses,Clothes") 

所以有人可以告訴我如何使這個值來像

IN("T-shirts","Evening Dresses","Dresses","Clothes") 

任何幫助和建議將是非常可觀的。由於

回答

1

快速的解決方案是:

改變您的片段:

$category_pool = ''; 
foreach($categoryArray as $categoryArr) 
    $category_pool .= $categoryArr.','; 
    $category_pool = ((strpos($category_pool, ',') === false) ? (' = '.$category_pool.' ') : (' IN ("'.rtrim($category_pool, ',').'") ')); 

一個我行:

$category_pool = 'IN ("' . implode('","',$categoryArray).'")'; 
+0

所以我沒有使用'$ category_pool。 = $ categoryArr。',';' – NewUser 2015-02-06 03:24:12

+0

我不知道什麼是'$ category_pool'代表:-) – Alex 2015-02-06 03:25:32

+0

我用你的代碼,但它的顯示它的顯示implode():傳遞的參數無效 代碼是這樣的 $ category_pool =((strpos($ category_pool,',')=== false)? ('='。$ category_pool。''):('IN(「'。implode('','',$ category_pool)。'」)')); – NewUser 2015-02-06 03:29:46

1
'IN(' 
. implode(
    ',', 
    array_map(function ($val) { 
     return sprintf('"%s"', $val); 
    }, $categoryArray) 
) 
. ')';