ninedoors Posted August 6, 2007 Share Posted August 6, 2007 Ok, I found this work around for array_combine on php.net. I need this because my server is only running php 4. So array_combine will not work as it is undefined. So here it is: function array_combine($arr1, $arr2) { $buffer = array(); foreach($arr1 as $key1 => $arr_1) { foreach($arr2 as $key2 => $arr_2) $buffer[$arr_1] = $arr_2; } return $buffer; } Now when I try to call this function I get an assoc. array but the values are incorrect. Here is thye example I used: $array_homepens = array(3, 5, 8, 24); $array_homepenmins = array(2, 2, 5, 4); $len = count($array_homepenalties); $assoc_array_hpen = array_combine($array_homepens, $array_homepenmins); print_r($assoc_array_hpen); And $assoc_array_hpen = Array ( [3] => 4 [5] => 4 [8] => 4 [24] => 4 ), which is clearly incorrect as it should be: Array ( [3] => 2 [5] => 2 [8] => 5 [24] => 4 ) Can anyone help me figure this out? Thanks Nick Link to comment https://forums.phpfreaks.com/topic/63599-solved-array_combine-work-around/ Share on other sites More sharing options...
Barand Posted August 6, 2007 Share Posted August 6, 2007 function array_combine ($keys, $data) { $res = array(); foreach ($keys as $i=>$k) { $res[$k] = $data[$i]; } return $res; } Link to comment https://forums.phpfreaks.com/topic/63599-solved-array_combine-work-around/#findComment-317030 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.