davidz Posted June 19, 2007 Share Posted June 19, 2007 I need to combine data from two arrays. Specifically I need the values from one array and the keys from another combined where the keys are the same. Example: [permissions] => Array ( [ACPlatSearch] => 3 [ACPropertySearch] => 11 [ACActiveDwellings] => 12 [ACUserInfo] => 13 [ACStatsRecordedSubs] => 16 [ACStats5yrTicker] => 17 [ACStatsAbsorption] => 29 [ACStatsTotalSales] => 28 [ACStatsSalesProduct] => 27 [ACStatsSalesDwellings] => 26 [ACStatsSalesLots] => 25 ) Plus: Array ( [ACStatsSalesDwellings] => on [ACStatsRecordedSubs] => on [ACStats5yrTicker] => on [ACStatsActiveDwellings] => on ) To end up like: Array ( [ACStatsSalesDwellings] => 26 [ACStatsRecordedSubs] => 16 [ACStats5yrTicker] => 17 [ACStatsActiveDwellings] => 12 ) Many thanks. Link to comment https://forums.phpfreaks.com/topic/56244-solved-combining-arrays/ Share on other sites More sharing options...
chigley Posted June 19, 2007 Share Posted June 19, 2007 http://www.php.net/array_merge Not sure how well it will work with identical keys.. Link to comment https://forums.phpfreaks.com/topic/56244-solved-combining-arrays/#findComment-277788 Share on other sites More sharing options...
akitchin Posted June 19, 2007 Share Posted June 19, 2007 try using one of the array intersection functions: http://ca.php.net/manual/en/function.array-intersect-key.php Link to comment https://forums.phpfreaks.com/topic/56244-solved-combining-arrays/#findComment-277789 Share on other sites More sharing options...
davidz Posted June 19, 2007 Author Share Posted June 19, 2007 Wow, thanks for the super fast replies. I've tried different iterations of array_merge, array_intersec, array_diff, etc. But I can only get it to show something like: Array ( [0] => ACStatsSalesDwellings [1] => ACStatsRecordedSubs [2] => ACStats5yrTicker [3] => ACStatsActiveDwellings ) Or the exact opposite of that. My next option is to do a foreach. Any thoughts? Link to comment https://forums.phpfreaks.com/topic/56244-solved-combining-arrays/#findComment-277799 Share on other sites More sharing options...
GingerRobot Posted June 19, 2007 Share Posted June 19, 2007 Well, as far as i'm aware, a key cant be duplicated in an array - if thats part of the outcome that you wanted(im not 100% sure ) then it's not going to work. Link to comment https://forums.phpfreaks.com/topic/56244-solved-combining-arrays/#findComment-277803 Share on other sites More sharing options...
cooldude832 Posted June 19, 2007 Share Posted June 19, 2007 try using the array_keys() and a counting function that get everything to line up nicely. like $keys = array_keys($array2); while ($i <= $numkeys) { $array3[$keys[$i]] = $array1[$i]; $i++; } something like that Link to comment https://forums.phpfreaks.com/topic/56244-solved-combining-arrays/#findComment-277806 Share on other sites More sharing options...
davidz Posted June 19, 2007 Author Share Posted June 19, 2007 Thanks for all the input! Here is what I went with: foreach ($_SESSION['permissions'] as $key1 => $value1){ foreach ($new_permissions as $key2 => &$value2){ if ($key1 == $key2){ $value2 = $value1; } } } Thanks again! Link to comment https://forums.phpfreaks.com/topic/56244-solved-combining-arrays/#findComment-277822 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.