ninedoors Posted August 6, 2007 Share Posted August 6, 2007 I am relatively new to php and I need to create an associative array from two other arrays. I thought I knew how to do it but I keep getting syntex errors. The two arrays I that I will be creating the associative array from are and will always be parrallel arrays. Hopefully this is possible because I want to be able to put the assoc. array through a foreach loop. Here is what I have, can someone tell me if I am even close? <? $array_homepenalties = array(3, 5, 8, 24); $array_homepenmins = array(2, 2, 5, 4); $len = count($array_homepenalties); for($i = 0; $i , $len; ++$i) { $assoc_array_hpen[ . $array_homepenalties[$i] . ] = [ . $array_homepenmins[$i] . ]; } print_r($assoc_array_hpen); ?> Thanks Nick Quote Link to comment https://forums.phpfreaks.com/topic/63560-solved-creating-associative-array-from-two-other-arrays/ Share on other sites More sharing options...
sasa Posted August 6, 2007 Share Posted August 6, 2007 try <?$array_homepenalties = array(3, 5, 8, 24); $array_homepenmins = array(2, 2, 5, 4); $len = count($array_homepenalties); for($i = 0; $i < $len; ++$i) { $assoc_array_hpen[ $array_homepenalties[$i] ] = $array_homepenmins[$i]; } print_r($assoc_array_hpen); ?> or use array_combine() function Quote Link to comment https://forums.phpfreaks.com/topic/63560-solved-creating-associative-array-from-two-other-arrays/#findComment-316764 Share on other sites More sharing options...
DeepakJ Posted August 6, 2007 Share Posted August 6, 2007 the PHP manual has a list of array functions that helped me out greatly. Check that site if you want and it may save you alot of time. Quote Link to comment https://forums.phpfreaks.com/topic/63560-solved-creating-associative-array-from-two-other-arrays/#findComment-316782 Share on other sites More sharing options...
ninedoors Posted August 6, 2007 Author 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 Quote Link to comment https://forums.phpfreaks.com/topic/63560-solved-creating-associative-array-from-two-other-arrays/#findComment-316903 Share on other sites More sharing options...
sasa Posted August 6, 2007 Share Posted August 6, 2007 look http://hr.php.net/array_combine Quote Link to comment https://forums.phpfreaks.com/topic/63560-solved-creating-associative-array-from-two-other-arrays/#findComment-316964 Share on other sites More sharing options...
ninedoors Posted August 6, 2007 Author Share Posted August 6, 2007 I can't use array_combine, I'm only running php 4. Quote Link to comment https://forums.phpfreaks.com/topic/63560-solved-creating-associative-array-from-two-other-arrays/#findComment-316977 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.