php从数组中获取值,就像格式一样

时间:2015-02-06 03:04:19

标签: php arrays sorting

我有一个像这样的数组

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")

任何帮助和建议都会非常明显。感谢

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分: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).'")';