Function/method to get selective columns
I'd suggest your code to be rewritten like this (please, take note of the inline comments):

&lt;?php

// Requires PHP &gt;= 5.4

public function getRows($ntt, $cols = array(), $where = array(), $result_type = null)
{
    $data = 'nulllllll';

    // Early return after checking arguments,
    // instead of nesting the rest of the code inside the if{}
    if (!($ntt &amp;&amp; $where &amp;&amp; is_array($where))) {
        return $data;
    }

    $filters = [];
    $param = [];

    // The callback function here uses outer variables: `$filters` and `$param`,
    // that are passed by reference `&amp;`
    array_walk($where, function($value, $key) use (&amp;$filters, &amp;$param) {
        static $i = 0;
        $i++;

        $placeholder = "v{$i}";
        $condition = is_array($value) ? "in (:{$placeholder})" : "= :{$placeholder}";
        $filters[] = "tm.{$key} {$condition}";
        $param[$placeholder] = $value;
    });

    // Use `implode` to concatenate the `$filters` into a string
    $filter = implode(' AND ', $filters);

    $select = 'tm';
    if (!empty($cols) &amp;&amp; is_array($cols)) {
        $select = implode(', ', array_map(function($column) {
                return "tm.{$column}";
            }, $cols));
    }

    $repository = "AppBundle:{$ntt}";
    $data = $this-&gt;em-&gt;getRepository($repository)-&gt;createQueryBuilder('tm')
        -&gt;select($select)
        -&gt;where($filter)
        -&gt;setParameters($param)
        -&gt;getQuery();

    if ($result_type === 'array') {
        return $data-&gt;getArrayResult();
    }

    return $data-&gt;getResult();
}