0

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

2 Answers 2

1

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).'")';
Sign up to request clarification or add additional context in comments.

4 Comments

so I don't have to use $category_pool .= $categoryArr.',';
I have no idea what is $category_pool stands for :-)
I used your code but its showing its showing implode(): Invalid arguments passed The code is like this $category_pool = ((strpos($category_pool, ',') === false) ? (' = '.$category_pool.' ') : ('IN ( "' . implode('","',$category_pool).'")'));
I've updated an answer. but still not understand how MY implode could get error message, by the way your foreach published is not completed... but I hope you can understand what you should do
1
'IN(' 
. implode(
     ',', 
     array_map(function ($val) {
         return sprintf('"%s"', $val);
     }, $categoryArray)
  ) 
. ')';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.