PHP - Array_merge Help
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 ) Similar TutorialsHi, I failed to merge arrays. This is the case. $arraylength= 2; for($i=0;$i<$arraylength;$i++){ $vars[] = array( $key1[$i] => $value1[$i], $key2[$i] => $avalue2[$i], $key3[$i] => $avalue3[$i]); } print_r($vars); I got then, for example, this output Code: [Select] Array( [0] => Array ( [key0] => value1, [key0] => value1, [key2] => value2 ) [1] => Array ( [key10] => value10, [key11] => value11, [key12] => value12 ) ) Then I need to merge those two arrays. It should be like array_merge($vars[0],$vars[1]);, but how can i merge more arrays? Thank you in advanced, ayok So I have two arrays (soon to be multiple). What I want to do is merge all those arrays into one, so that I can then proceed to inserting the data all together. Here's an example of what array1 looks like. Code: [Select] Array ( [0] => Array ( [name] => samus1 [forum] => phpfreaks.com ) [1] => Array ( [name] => samus2 [forum] => stackoverflow.com ) ) And array2: Code: [Select] Array ( [0] => Array ( [posts] => 25 ) [1] => Array ( [posts] => 2500 ) ) I then tried to do: Code: [Select] print_r(array_merge($array1, $array2)); Yet it prints nothing? When I print the other arrays individually the values are shown, but after I use array_merge() it doesn't. Anyone got an idea why? Here's the code I am working with: Code: [Select] <?php echo " cupcakes, ".$_SESSION['delcol'].", with the following toppings: "; $array1 = $_SESSION['notopping']; $array2 = $_SESSION['topping']; $resultam = array_merge($array1, $array2); $last = array_pop($resultam); $topping = implode(', ', $resultam) .' & ' .$last; echo $topping; ?> I have two arrays which I want to join together, if $_SESSION['notopping'] this can contain 1 - 6. $_SESSION['topping'] can contain vanilla, chocolate or chocolate orange. What I'm trying to achieve is to merge the arrays so that it shows like 1 x Vanilla, 2 x Chocolate & 3 x Choc Orange. As it was it was formatted so it showed just the toppings (with commas and ampersand). The code above appears as this : 1, 2, 3, Vanilla, Chocolate & Choc Orange - the two arrays are separated out, what am I missing here? Thanks! I want to merge some arrays as Code: [Select] $mixing = array_merge($mixedresult1, $mixedresult2, $mixedresult3);but the arrays can be empty sometimes. In this case, I will get an error for the empty array as Code: [Select] PHP Warning: array_merge() [<a href='function.array-merge'>function.array-merge</a>]: Argument #1 is not an arrayHow I can avoid this error by omitting empty arrays? I am trying to understand why the following code outputs the value of 4 (it is part of a PHP quiz question). I know that array_diff will compare one array with another and return the result but I just don't understand what is going on with the code below: Code: [Select] <?php $a = array(1,2,4,8); $b = array(0,2,4,6,8,10); echo count (array_merge(array_diff($a, $b), array_diff($b, $a))); //prints 4 ?> Would someone be able to walk me through it? |