How to sort array as same key value using PHP -


i need 1 help. need sort array per key value using php.

i explaining array below.

$clients=array(array("clinet_id"=>9,"company"=>3,"id"=>4),array("clinet_id"=>9,"company"=>3,"id"=>6),array("clinet_id"=>9,"company"=>3,"id"=>7),array("clinet_id"=>10,"company"=>2,"id"=>4),array("clinet_id"=>10,"company"=>2,"id"=>8)); echo json_encode($clients); 

here need sort array per client_id means same client_id merge 1 array id comma separated string. explaining expected output below.

$templatearr[9]=array('3' =>4,6,7) $templatearr[10]=array('2' =>4,8) 

means should $templatearr[clinet_id]=array(company =>id1,id2,...,) format. please help.

code this:

$tmp = array(); foreach ($clients $k => $v) {     if(!isset($tmp[$v['clinet_id']])){         $tmp[$v['clinet_id']] = '';     }     if(!is_array($tmp[$v['clinet_id']])){         $tmp[$v['clinet_id']][$v['company']] =  strval($v['id']);     }else{         if($tmp[$v['clinet_id']][$v['company']]){             $tmp[$v['clinet_id']][$v['company']] .=','.$v['id'];         }else{             $tmp[$v['clinet_id']][$v['company']] =  strval($v['id']);         }     } } 

and result is:

array (     [9] => array         (             [3] => 4,6,7         )      [10] => array         (             [2] => 4,8         )  ) 

Comments

Popular posts from this blog

html - How to set bootstrap input responsive width? -

javascript - Highchart x and y axes data from json -

javascript - Get js console.log as python variable in QWebView pyqt -