Jump to content

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/254276-array_merge-issues/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/254276-array_merge-issues/#findComment-1303739
Share on other sites

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!

Link to comment
https://forums.phpfreaks.com/topic/254276-array_merge-issues/#findComment-1303747
Share on other sites

$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

Link to comment
https://forums.phpfreaks.com/topic/254276-array_merge-issues/#findComment-1303755
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/254276-array_merge-issues/#findComment-1303836
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/254276-array_merge-issues/#findComment-1303842
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/254276-array_merge-issues/#findComment-1303843
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/254276-array_merge-issues/#findComment-1303845
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/254276-array_merge-issues/#findComment-1303851
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/254276-array_merge-issues/#findComment-1303872
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/254276-array_merge-issues/#findComment-1304230
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.