petroz Posted September 14, 2010 Share Posted September 14, 2010 Hi Guys, Been a while since I've done this and I cant seem to get it right. I am using an array to create a dropdown menu. The first array is all options, and the second array is the selected option. Here is the code I am using to merge the arrays. //create the dropdown $possible_pages = $this->admin_model->page_dropdown(); $current_page = $this->admin_model->current_page_dropdown($page_id); $data['pages_dropdown'] = array_merge($current_page, $possible_pages); The result I am getting seems to be reseting the keys in the array, which will not work as intended. Please point me in the right direction to get this array to look as intended the in the example below. I would like the array to look like... Array ([8] => VPO's [7] => CPO's [8] => VPO's) Here are the arrays... Array ( [7] => CPO's [8] => VPO's ) Array ( [8] => VPO's ) This is the result I am getting now. Array ( [0] => VPO's [1] => CPO's [2] => VPO's ) Link to comment https://forums.phpfreaks.com/topic/213436-array_merge-help/ Share on other sites More sharing options...
btherl Posted September 14, 2010 Share Posted September 14, 2010 Each index can only appear once in an array, so you'll just have one 7 and one 8 in the result array, even if you merge without reindexing. I think what you are looking for is the "+" array operator. It's mentioned at http://php.net/manual/en/function.array-merge.php Link to comment https://forums.phpfreaks.com/topic/213436-array_merge-help/#findComment-1111187 Share on other sites More sharing options...
petroz Posted September 14, 2010 Author Share Posted September 14, 2010 Hi btherl, Thanks for the reply. I just tried using the union operator. But its not working. Take a look below The revised code... //create the dropdown $possible_pages = $this->admin_model->page_dropdown(); $current_page = $this->admin_model->current_page_dropdown($page_id); $data['pages_dropdown'] = array_merge($current_page + $possible_pages); I also tried this... and I get the same result as the above code... $data['pages_dropdown'] = $current_page + $possible_pages; Result.. Array ( [8] => VPO's [7] => CPO's ) Thanks Again for the reply! Link to comment https://forums.phpfreaks.com/topic/213436-array_merge-help/#findComment-1111191 Share on other sites More sharing options...
Hypnos Posted September 15, 2010 Share Posted September 15, 2010 I would like the array to look like... Array ([8] => VPO's [7] => CPO's [8] => VPO's) Creating an array like this is impossible. Keys have to be unique. You have 8 twice. Result.. Array ( [8] => VPO's [7] => CPO's ) That's as close as you will get. Link to comment https://forums.phpfreaks.com/topic/213436-array_merge-help/#findComment-1111210 Share on other sites More sharing options...
petroz Posted September 15, 2010 Author Share Posted September 15, 2010 Thanks guys. That option will work fine! Link to comment https://forums.phpfreaks.com/topic/213436-array_merge-help/#findComment-1111218 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.