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'); Link to comment https://forums.phpfreaks.com/topic/215297-create-a-2d-array-from-2-single-d-array/ 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 Link to comment https://forums.phpfreaks.com/topic/215297-create-a-2d-array-from-2-single-d-array/#findComment-1119621 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 ) Link to comment https://forums.phpfreaks.com/topic/215297-create-a-2d-array-from-2-single-d-array/#findComment-1119627 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); Link to comment https://forums.phpfreaks.com/topic/215297-create-a-2d-array-from-2-single-d-array/#findComment-1119642 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. Link to comment https://forums.phpfreaks.com/topic/215297-create-a-2d-array-from-2-single-d-array/#findComment-1119795 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 Link to comment https://forums.phpfreaks.com/topic/215297-create-a-2d-array-from-2-single-d-array/#findComment-1120269 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.