Hyperjase Posted January 3, 2012 Share Posted January 3, 2012 Here's the code I am working with: <?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! Quote Link to comment https://forums.phpfreaks.com/topic/254276-array_merge-issues/ Share on other sites More sharing options...
SergeiSS Posted January 3, 2012 Share Posted January 3, 2012 It seems that you need array_combine() http://ru.php.net/manual/en/function.array-combine.php Also you have to change your code, because if you use array_combine() you'd have one array as array's keys and another as it's values. As for array_merge... It works as expected But you don't need it here. Quote Link to comment https://forums.phpfreaks.com/topic/254276-array_merge-issues/#findComment-1303739 Share on other sites More sharing options...
Hyperjase Posted January 3, 2012 Author Share Posted January 3, 2012 Hmmm, yeah when I was researching with one to use I thought array_combine would be better but it doesn't work .... when I change array_merge to array_combine, this is all it displays : & Chocolate When I use it with array_merge it displays : 1, 1, Vanilla & Chocolate - so that appears to work better? Maybe I've formatted wrong within PHP but I'm in unknown territory for me! Quote Link to comment https://forums.phpfreaks.com/topic/254276-array_merge-issues/#findComment-1303747 Share on other sites More sharing options...
ManiacDan Posted January 3, 2012 Share Posted January 3, 2012 $a = array(2,6); $b = array('chocolate', 'orange'); $c = array_combine($b, $a); print_r($c); /*Array ( [chocolate] => 2 [orange] => 6 )*/ foreach ( $c as $topping => $amount ) { echo "{$amount} servings of {$topping}\n"; } /*2 servings of chocolate 6 servings of orange*/ -Dan Quote Link to comment https://forums.phpfreaks.com/topic/254276-array_merge-issues/#findComment-1303755 Share on other sites More sharing options...
Hyperjase Posted January 3, 2012 Author Share Posted January 3, 2012 Thanks - that was my back up plan to do it that way, I'm keen on trying to keep it so it has commas and ampersand (easier to read). Is that possible with the code I posted? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/254276-array_merge-issues/#findComment-1303822 Share on other sites More sharing options...
Hyperjase Posted January 3, 2012 Author Share Posted January 3, 2012 Using this code (I had to change variable $a and $b) : <?php $a = array($_SESSION['notopping']); $b = array($_SESSION['topping']); $c = array_combine($b, $a); print_r($c); foreach ( $c as $topping => $amount ) { echo "{$amount} servings of {$topping}\n"; } ?> I get this output - ( [Array] => Array ( [ 0 ] => 1 [1] => 3 [2] => 5 ) ) which is right on one level (it got the numbers correct) but it's missing the array from the topping name. Am I missing something? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/254276-array_merge-issues/#findComment-1303836 Share on other sites More sharing options...
ManiacDan Posted January 3, 2012 Share Posted January 3, 2012 $_SESSION['topping'] and $_SESSION['notopping'] are presumably already arrays, so you don't need to put them inside MORE arrays. Also, you can format your output however you want, I just spit out whole sentences. Quote Link to comment https://forums.phpfreaks.com/topic/254276-array_merge-issues/#findComment-1303839 Share on other sites More sharing options...
Hyperjase Posted January 3, 2012 Author Share Posted January 3, 2012 I did try withot the array() but I get this error : Warning: array_combine() [function.array-combine]: Both parameters should have an equal number of elements - not sure where I'm going wrong here, to be honest all I need to do now is just make it so I can output the number then the topping, so I can insert into mysql. Cheers Jason Quote Link to comment https://forums.phpfreaks.com/topic/254276-array_merge-issues/#findComment-1303842 Share on other sites More sharing options...
ManiacDan Posted January 3, 2012 Share Posted January 3, 2012 Well...both arrays need to have an equal number of elements. Are you telling me that you're going to have a list of 2,3,4 and another list of Chocolate,Orange, and you're going to somehow make the computer figure out that they wanted 2 of Chocolate and 3 of Orange and 4 of...what? How, on paper, would you take "2,3,4" and "Chocolate,Orange" and get the output you're looking for? Maybe we're not understanding what you actually want. It seemed, up until just now, that you had an array of "quantities required" and an array of "type required" and you just needed to put them together. Quote Link to comment https://forums.phpfreaks.com/topic/254276-array_merge-issues/#findComment-1303843 Share on other sites More sharing options...
Hyperjase Posted January 3, 2012 Author Share Posted January 3, 2012 The system as it is - you choose how many toppings there are (possible max 6) then next page, it dynamically creates a drop down box for each selected topping. All I need to do is associate the number to the topping. Why it appears to be missing one topping I'm not quite sure. This is the deepest I've got into php yet, I'm still on my learning curve. My apologies if my knowledge of php is someone seriously limited. I know the code I've already done will be messy & not to great standards but so far it works. I can send you a copy of the entire script as it stands now if that's any help? Thanks for your help on this, Jason Quote Link to comment https://forums.phpfreaks.com/topic/254276-array_merge-issues/#findComment-1303845 Share on other sites More sharing options...
ManiacDan Posted January 3, 2012 Share Posted January 3, 2012 Ok then you're totally doing it wrong. In the form that creates the drop-downs, NAME THEM AFTER THE TOPPING. Look: <form method="POST"> <select name="toppings[chocolate]"> <option value=1>1</option> <option value=1>2</option> <option value=1>3</option> </select> <select name="toppings[orange]"> <option value=1>1</option> <option value=1>2</option> <option value=1>3</option> </select> <input type="submit" /> </form> When you submit this form, $_POST['toppings'] will be an array with the key being the topping name, and the value being what they chose from the drop-down. If you have full control of this data, there's no reason for you to be trying to associate random parallel arrays. Quote Link to comment https://forums.phpfreaks.com/topic/254276-array_merge-issues/#findComment-1303851 Share on other sites More sharing options...
Hyperjase Posted January 3, 2012 Author Share Posted January 3, 2012 I follow now, I've managed to sort it dynamically for the drop down as prior to all of this, you can choose either 6, 12, 24 or 36 - each being dynamically created into a drop down (with the selected number). Basically, if you choose 6 cupcakes, two toppings - you must select two toppings which total 6, same for 12, 24 etc. I have this to do with another option too but I will get this fully functioning first. So now I have this in place, the toppings array will be more useable for what I need it for? Will experiment now with this lot. Thanks so much! Jason Quote Link to comment https://forums.phpfreaks.com/topic/254276-array_merge-issues/#findComment-1303872 Share on other sites More sharing options...
Hyperjase Posted January 4, 2012 Author Share Posted January 4, 2012 Here's the code I have to output using the recommended solution above <?php foreach($_SESSION['notopping'] as $key => $value) { $_SESSION['topping2'] = " $value x $key "; echo $_SESSION['topping2']; } ?> How do I use this as one array (outside of the foreach it will only display the final value in the array). I need to insert this all into a database. Thanks, Jason Quote Link to comment https://forums.phpfreaks.com/topic/254276-array_merge-issues/#findComment-1304230 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.