0
foreach ($_SESSION['exampleOne'] as $item)
{//insert sql

foreach ($_SESSION['exampleTwo'] as $comment)
{//insert sql

How do I merge 2 foreach together? now my database store like this.. :

    comment | item
    ---------------
    Nice!   | Pants
    Great!  | Pants
    Awesome!| Pants
    Nice!   | Skirts
    Great!  | Skirts
    Awesome!| Skirts
    Nice!   | Shirts
    Great!  | Shirts
    Awesome!| Shirts

I want it somehow to be like this in my database when I store it:

comment | item
---------------
Nice!   | Pants
Great!  | Skirts
Awesome!| Shirts
5
  • 2
    can you post 'var_dump($_SESSION["exampleOne"])' for us Commented Dec 31, 2012 at 9:05
  • I don't understand. Can you show the values of your $_SESSION arrays? Commented Dec 31, 2012 at 9:05
  • You've basically omitted most of the relevant information: what your arrays contain and what your SQL statements look like. Commented Dec 31, 2012 at 9:05
  • $_SESSION['exampleOne'] is having comments and $_SESSION['exampleTwo'] is having item ? Commented Dec 31, 2012 at 9:06
  • The questions are in your mind only still.. Please express it Commented Dec 31, 2012 at 9:07

1 Answer 1

1

You can do like this--

$merge = array_combine ($_SESSION['exampleOne'],$_SESSION['exampleTwo'])
foreach($merge as $key => $value)
{
    // your insert query
    $query = "insert into tableName set comment='$key', item='$value'";
}
Sign up to request clarification or add additional context in comments.

2 Comments

I still don't know what OP is looking for. This seems correct, except I think the SESSION arrays might need to be boiled down to their unique values with array_unique, according to the example in the question. Still, though, it doesn't make sense to me. :)
Beware of the SQL injection!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.