Ok, I am back, and still lost in array land.

I am now trying to merge 2 arrays so I can do 2 inserts with the key of one array and the value from another.

I have (again) products array:

products [1] = 1258
products [6] = 1201 

and then a comment for each (comments array)

comments [1] = 'comment for productId 1258'
comments [6] = 'comment for productId 1201' 

But I tried this approach and I get the data inserted twice (and I cant see how to get the data without looping thru the 2 arrays, and not get duplicate inserts).

foreach( $_SESSION['products'] as $productId =>  $productkey ) {
  foreach( $_SESSION['comments'] as $commentsId => $commentskey) {
      $sql = "INSERT INTO table (productkey , commentskey) VALUES ($productkey , $commentskey)";
   }
} 

I hope I can eventually get my brain 'wrapped around' arrays.

Thanks,
d.

    Hi,

    Haven't seen your earlier posts so I might be missing something but since both arrays have the same indices (from your example), this should work ...

    <?php
    foreach( $_SESSION['products'] as $productId =>  $productkey ) {
    	//foreach( $_SESSION['comments'] as $commentsId => $commentskey) {
    	$sql = 'INSERT INTO table (productkey , commentskey) VALUES ('.$productkey.' , '.$_SESSION['comments'][$productId].')';
    	//}
    }
    ?>

    Just loop through one of the two arrays, and use the 'loop' index in the second array.

    Paul.

      You get the data inserted twice in your example since you use an array with two values. Try 3....will get inserted 3 times. And also have a look: the data gets mixed. You do what is known in database table joins as a "cartesian join".

      Reason is that the inner foreach loops runs completely for every single element of the outer loop. That's not what you want.

      There are two things that you can do:
      1) rip the arrays apart with array_keys() and array_values() - this leaves you with numerically indexed arrays that you can loop through with a simple for($i = 0....

      2)advance the inner array with next() and use current()/key() to retrieve the value/key like so:

      foreach($_SESSION['products'] as $productId =>  $productkey) { 
          next($_SESSION['comments']);
          $commentskey = key($_SESSION['comments']);
          $sql = "INSERT INTO table (productkey , commentskey) VALUES ($productkey , $commentskey)"; 
      } 

        Hi Bjom,

        Be a little careful with

        foreach($_SESSION['products'] as $productId =>  $productkey) {
            next($_SESSION['comments']);
            $commentskey = key($_SESSION['comments']);
            $sql = "INSERT INTO table (productkey , commentskey) VALUES ($productkey , $commentskey)";
        } 

        ... since it requires that both session array keys are ordered in the same way - which might not be the case.

        P 😉

          That is true - that's always a big risk when operating on two arrays. You have to rely on either the keys to match or the order. None of which must be the case 😉

          array_intersect()
          array_sort()

          can help - but usually it is simply better to avoid splitting the data up into two arrays in the first place.

            Thanks paulnaj and Bjom,

            I took paulnaj's code and it worked perfectly.

            I also appreciate the explanations, I like to understand why and how, and not just take the code verbatim and use it. It helps me learn and ask less questions (at least for most stuff, but array's are taking a bit longer for me.)

            Thanks for you help and the training lesson.
            D.

              Write a Reply...