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. Quote Link to comment 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.. Quote Link to comment 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 Quote Link to comment 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? Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment 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! 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.