View Single Post
Old 11-26-2012, 07:46 PM   PM User | #4
Ulysses69
New Coder

 
Join Date: May 2004
Posts: 15
Thanks: 1
Thanked 0 Times in 0 Posts
Ulysses69 is an unknown quantity at this point
Thanks, that works as desired.

Is there however, an alternative to array_merge function, so that the appended array data is pushed to the array? as it is, the $cart array stays the original size and not representive of the multiple additional entires that I was after, despite these entries properly populating the $rows variable in the example.

PHP Code:
<?php
$groupsize 
3;  // OR WHAT EVER NUMBER YOU WANT
$cart = array("Apple","Pear","Banana","Peach","Orange","Coconut","Melon","Berries","Watermelon");
$test $cart;
$count 1;
while(
count($cart)%$groupsize  != 0) {
    
//$cart = array_merge($cart, $test); // $TEST WILL ALWAYS BE THE SAME BEGING ARRAY
    
$cart $cart $test;
    
//print_r($cart);
    //echo "<br />".count($cart)."<br /><br />";
    
$count++;
}

foreach (
$test as $row):
    
$rows .= $row."\n";
endforeach;
echo 
'<h2>Original List</h2><pre>'.$rows.'</pre>';
echo 
sizeof($test);

$i=0;
foreach (
$cart as $row):
    
$i++;
    
$rows .= $row."\n";
    if (
$i $groupsize == 0){
    
$rows .= '-<br/>';
    }
endforeach;

echo 
'<h2>Amended List</h2><pre>'.$rows.'</pre>';
echo 
sizeof($cart);
?>
I am grateful for your help, and am just wondering if there is an alternative to array_merge to do this, without creating a new array just for this purpose.

Last edited by Ulysses69; 11-26-2012 at 07:59 PM..
Ulysses69 is offline   Reply With Quote