Why my php array is over written when they have same Keys -
i have custom php function executes stored procedure , returns array:
function test(){ $in = array("abc","bcd","efg"); $result = mydba->executestoredprocedure('proc1',$in); $arr_sim = array(); foreach ($result['recordset'] $rows) { if (!empty($rows)) { echo $arr_sim[$rows['field1']] = $rows['field2']; } } return $arr_sim; } in above function $arr_sim returning number of items correctly when rows["field1"] values different. if rows["field1"] values same overwriting first value , returning last one. how can overcome this?
array ( [chicago] => 'sears', [rochester] => 'liberty' ) if $arr_sim contains these items returned correctly. because keys different.
array ( [chicago] => 'mcd', [chicago] => 'tacobell' ) if $arr_sim contains these items not returned correctly. because keys same, "chicago".
array keys must unique. instead, this:
// want array // array('chicago' => array('mcd', 'tacobell')); function test(){ $in = array("abc","bcd","efg"); $result = mydba->executestoredprocedure('proc1',$in); $arr_sim=array(); foreach ($result['recordset'] $rows) { if(!empty($rows)){ if(array_key_exists($rows['field1'], $arr_sim) { $arr_sim[$rows['field1']][] = $rows['field2']; } else { $arr_sim[$rows['field1']] = array($rows['field2']); } } } return $arr_sim; }
Comments
Post a Comment