tags:

views:

72

answers:

6
myarray[] = $my[$addintomtarray]  
//52 elements  
for ($k=0; $k <= 12; $k++){  
    echo  $myarray[$k].' ';  
}  
echo '<br>';   
for ($k=13; $k < 26; $k++){  
    echo  $myarray[$k].' ';  
}  
echo '<br>';   
for ($k=26; $k < 39; $k++){  
    echo  $myarray[$k].' ';  
}  
echo '<br>';  
for ($k=39; $k <= 51; $k++){  
    echo  $myarray[$k].' ';  
}   

how to shorten this array code...all I am doing here is splitting an array of 52 elements into 4 chunck of 13 elements each. In addition I am adding formation with br and space

thanks

+8  A: 

Use the modulus operator (%) to know when you are at a multiple of 13:

for ($k=0; $k <= 51; $k++){  
    echo  $myarray[$k].' ';
    if (($k > 0) && (($k % 13) == 0) {
        echo '<br>';
    }
} 
R Samuel Klatchko
A: 

Loop through all elements in one loop. Use a % comparison conditionally.

~Edit~ See Mr. Klatchko's code below.

Jan Kuboschek
A: 

You definitely don't need to do that many for loops:

$myarray[] = $my[$addintomtarray];
//52 elements

$i = 1;
foreach( $myarray as $v ){
    echo "$v ";

    if( 0 == $i % 13 )
        echo '<br />';

    $i++;
}  
Kerry
If you mark down my response, I would like to know why.
Kerry
+1  A: 

A better way to do this might be to use the array_slice function.

From the docs:

array array_slice ( array $array , int $offset [, int $length [, bool $preserve_keys = false ]] )

"array_slice() returns the sequence of elements from the array array as specified by the offset and length parameters."

misterMatt
How would this be better? I don't see it.
Jan Kuboschek
@Jan Kuboschek This method may be easier to read, and alternatively he would be able to easily save these values in new arrays if he decided to do that instead of printing them. I suppose if he wanted to do stuff with the 'chunks' this might be the way to go.
misterMatt
I'm short on time so I'm not 100% both sets of code do what he needs... But, if they do, you definitely want to use the array_slice method, for performance. PHP array builtin methods are usually just wrappers for the C level functions, which means they're lightning fast. A foreach loop is glacial in comparison. I try to always use builtins when they do what I need.
Travis Leleu
A: 

I came up with this this idiom just yesterday to prevent flooding by my web crawler.

$myarray[] = $my[$addintomtarray]  

// ...

// NOTE: This modifies $myarray!  Make a copy of it first if you
// need to (e.g. by making this its own function and passing by-value).

while(($line = array_splice($myarray, 0, 13))) {
    echo implode(' ', $line);

    if(count($myarray) !== 0) {
        echo '<br/>';
    }
}
strager
A: 

I've always preferred to use array_chunk(). Once I get the array distributed in its raw form, I can display it any way I want.

array_chunk( $myarray, 13 );

Now you have a 4 element array, each element of which contains a 13 element array. A simple nested loop will allow you to iterate and display in whatever manner you choose.

Rob Wilkerson