alvaronicardo Posted October 6, 2010 Share Posted October 6, 2010 Hello can anyone help me out with code to create a 2 dimension array from 2 single dimension array. for example $path = array('base', 'category', 'subcategory', 'item'); $location=array('india','USA','UK','RUSSIA',); now i need to have a @D array which have the following structure $final[0]=array('base','india'); $final[1]=array('category','USA'); $final[2]=array('subcategory','UK'); $final[3]=array('item','RUSSIA'); Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted October 6, 2010 Share Posted October 6, 2010 Use a simple "for" loop: <?php $path = array('base', 'category', 'subcategory', 'item'); $location=array('india','USA','UK','RUSSIA'); $final = array(); for ($i=0;$i<count($path);++$i) { $final[] = array($path[$i],$location[$i]); } ?> Ken Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 6, 2010 Share Posted October 6, 2010 Might I suggest an alternative solution that "may" fit your needs better? The values you specified in your example may just be "mock" data that is not representative of the values you are really using. But, at first I thought that the first list of values were "labels" and the second list were "values", but I'm not sure. At the very least you are creating a one-to-one relationship. In that case, a better approach may be to create a single dimension array where one list is used as the keys and the other list is used as the values: $final = array(); for($i=0, $count=min(count($path), count($location)); $i<$count; ++$i) { $final[$path[$i]] = $location[$i]; } Content of $final Array ( [base] => india [category] => USA [subcategory] => UK [item] => RUSSIA ) Quote Link to comment Share on other sites More sharing options...
sasa Posted October 7, 2010 Share Posted October 7, 2010 if you like majdamato's solution you can use function array_combine($keysarray, $valuesarray); Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 7, 2010 Share Posted October 7, 2010 if you like majdamato's solution you can use function array_combine($keysarray, $valuesarray); I knew there had to be a function for doing that but couldn't find it. Quote Link to comment Share on other sites More sharing options...
alvaronicardo Posted October 8, 2010 Author Share Posted October 8, 2010 Thank mjdamato, you save my day,thats the way i was looking for Quote Link to comment 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.