简体   繁体   中英

php get the values from the array as like format

I have an array like this

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

Here I want to get the values of the array and add "" for each values and add , after each value So the valus should finally come like this

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

so for this I made my code like this

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

But here it is getting the value like

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

So can someone tell me how to make this value to come like

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

Any help and suggestions will be really appreciable. Thanks

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

Quick solution is:

change your fragment:

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

with one my line:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM