jruzafa
4/8/2013 - 9:03 AM

Convert array php to js array

Convert array php to js array

public function convertToJsArray($name = null, $data){

        if(is_null($name)){
            $arrayJs = "var countriesReq = [ ";
        }else{
            $arrayJs = "var $name = [ ";
        }

        for($i = 0; $i < count($data); $i++){

            if($i == count($data)-1){
                $arrayJs .= "\"$data[$i]\"];";
            }else{
                $arrayJs .= "\"$data[$i]\",";
            }
        }

        return $arrayJs;
    }