Convert array to quoted string

I have an array

$array = ["Tony","Steve","Peter","Steven","Bruce"];

I want to create a function which returns a query

SELECT * FROM avengers where name in ("Tony","Steve","Peter","Steven","Bruce");


Tagged:

Best Answer

  • sachin
    Accepted Answer
    <?php
    $array = array("Tony","Steve","Peter","Steven","Bruce");
    
    function query($array){	
        $ar_user  = array_map( function($str){ return sprintf("\"%s\"", $str); }, $array);
        $ar_list  = implode(",",$ar_user);
        $query    = "SELECT * FROM avengers where name in (".$ar_list.")";
        return $query;
    }
      
    echo query($array);
    ?>
    
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!