Function/method to get selective columns Is it possible to simplify getRows() or it is fine as it is now?

protected $container;
public function __construct(ContainerInterface $container) {
    $this-&gt;container = $container;
    $this-&gt;em = $this-&gt;getDoctrine()-&gt;getManager();
}

/*
* @param    $ntt              name of the entity to fetch
* @param    $cols             columns to fetch
* @param    $where            array of data to be matched (for example array('id'=&gt;1))
* @param    $result_type      type of result required. (object|array)
*/
public function getRows($ntt,$cols=array(),$where=array(),$result_type=null){
    $data = 'nulllllll';
    if($ntt &amp;&amp; $where &amp;&amp; is_array($where)){
        $i = 1;
        $filter = '';
        foreach ($where as $key =&gt; $value) {
            if(is_array($value)){
                $filter = $filter.($filter ? ' AND ' : '')."tm.$key in (:v$i)";
            }else{
                $filter = $filter.($filter ? ' AND ' : '')."tm.$key = :v$i";
            }
            $param['v'.$i] = $value;
            $i++;
        }

        $select = 'tm';
        if($cols){
            $select = '';
            foreach ($cols as $col) {
                $select = $select.($select ? ', ' : '')."tm.$col";
            }
        }

        $ntt = 'AppBundle:'.$ntt;
        $data = $this-&gt;em-&gt;getRepository($ntt)-&gt;createQueryBuilder('tm')
        -&gt;select($select)
        -&gt;where($filter)
        -&gt;setParameters($param)
        -&gt;getQuery();
        if($result_type == 'array'){
            $data = $data-&gt;getArrayResult();
        }else{
            $data = $data-&gt;getResult();
        }
    }
    return $data;
}


Calling:

getRows('My_user',array('lName','llname'),array('id'=&gt;array(1,4,50)),'array')


Where My_user is my entity name and lName and llname are the columns I want to fetch.

Will output:

Array
(
    [0] =&gt; Array
        (
            [lName] =&gt; Abc
            [llname] =&gt; Sha
        )

    [1] =&gt; Array
        (
            [lName] =&gt; And
            [llname] =&gt; Bainbri
        )

    [2] =&gt; Array
        (
            [lName] =&gt; Ken
            [llname] =&gt; Lee
        )

)
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();
}