champton20 Posted January 27, 2010 Share Posted January 27, 2010 I need to merge arrays dynamically. Here is the scenerio: I have a main array that I produce from form variables, then based on database rows that relate to that user, I have to create additional arrays. I then will create a final array from array_merge. The problem is I could have numerous rows and I dont want to use if else statements to write that final array_merge. Is there a way to create my final $array dynamically without using if statements? Thanks in advance for your help. Here is my code: $cartItems = mysql_query("SELECT * FROM carts WHERE basketSession = '".$basketSession."'"); $rows = mysql_num_rows($cartitems); $a = "array_cart"; $x = 0; while($r = mysql_fetch_array($cartItems)) { ${$a.$x} = array( 'cart.Cart.CartItems['.$x.'].Id'=> $cartId, 'cart.Cart.CartItems['.$x.'].CartId' => $cartId, 'cart.Cart.CartItems['.$x.'].Sku' => $r['idProduct'], 'cart.Cart.CartItems['.$x.'].Quantity' => $r['quantity'], 'cart.Cart.CartItems['.$x.'].ItemAmount' => $r['donation'], 'cart.Cart.CartItems['.$x.'].AllowCartResponse' => 'false', 'cart.Cart.CartItems['.$x.'].Amount' => $r['donation'], 'cart.Cart.CartItems['.$x.'].Description' => $r['productName'].'-'.$r['productOption']); $x++; } if($rows == 1) { $array = array_merge($main_array, $array_cart0); } elseif($rows == 2) { $array = array_merge($main_array, $array_cart0, $array_cart1); } elseif($rows == 3) { $array = array_merge($main_array, $array_cart0, $array_cart1, $array_cart2); } else($rows == 4) { $array = array_merge($main_array, $array_cart0, $array_cart1, $array_cart2, $array_cart3); } Link to comment https://forums.phpfreaks.com/topic/190002-help-with-arrays/ Share on other sites More sharing options...
Catfish Posted January 27, 2010 Share Posted January 27, 2010 why not just build the main array in the while loop?: while($r = mysql_fetch_array($cartItems)) { $mainArray[$a.$x] = array( 'cart.Cart.CartItems['.$x.'].Id'=> $cartId, 'cart.Cart.CartItems['.$x.'].CartId' => $cartId, 'cart.Cart.CartItems['.$x.'].Sku' => $r['idProduct'], 'cart.Cart.CartItems['.$x.'].Quantity' => $r['quantity'], 'cart.Cart.CartItems['.$x.'].ItemAmount' => $r['donation'], 'cart.Cart.CartItems['.$x.'].AllowCartResponse' => 'false', 'cart.Cart.CartItems['.$x.'].Amount' => $r['donation'], 'cart.Cart.CartItems['.$x.'].Description' => $r['productName'].'-'.$r['productOption']) $x++; } if you want each item numerically indexed in the array, change: $mainArray[$a.$x] to $mainArray[$x][$a.$x] Link to comment https://forums.phpfreaks.com/topic/190002-help-with-arrays/#findComment-1002502 Share on other sites More sharing options...
champton20 Posted January 27, 2010 Author Share Posted January 27, 2010 Actually the $main_array is created above the code I originally posted, but I figured it out. I just merged the $main_array in the loop and then assigned the final array after the loop. $cartItems = mysql_query("SELECT * FROM carts WHERE basketSession = '".$basketSession."'"); $x = 0; while($r = mysql_fetch_array($cartItems)) { $temp_array = array( 'cart.Cart.CartItems['.$x.'].Id'=> $cartId, 'cart.Cart.CartItems['.$x.'].CartId' => $cartId, 'cart.Cart.CartItems['.$x.'].Sku' => $r['idProduct'], 'cart.Cart.CartItems['.$x.'].Quantity' => $r['quantity'], 'cart.Cart.CartItems['.$x.'].ItemAmount' => $r['donation'], 'cart.Cart.CartItems['.$x.'].AllowCartResponse' => 'false', 'cart.Cart.CartItems['.$x.'].Amount' => $r['donation'], 'cart.Cart.CartItems['.$x.'].Description' => $r['productName'].'-'.$r['productOption']); $x++; $main_array = array_merge($main_array, $temp_array); } $array = $main_array; Link to comment https://forums.phpfreaks.com/topic/190002-help-with-arrays/#findComment-1002511 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.