Jump to content

array_merge help


petroz

Recommended Posts

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.