Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-11-2010, 03:30 AM   PM User | #1
MattyUK
Regular Coder

 
Join Date: Jul 2002
Posts: 301
Thanks: 7
Thanked 2 Times in 2 Posts
MattyUK is on a distinguished road
Question Split indexed single dimension array into groups of X (2 dimension array)

If I have a single dimensioned indexed array that I want split into groups what is the best way to do this?

If I have this:
Code:
Array
(
    [0] => Value0
    [1] => value1
    [2] => value2
    [3] => value3
    [4] => value4
    [5] => value5
    [6] => value6
    [7] => value7
    [8] => value8
    [9] => value9
    [10] => value10
    [11] => value11
    [12] => value12
    [13] => value13
    [14] => value14
    [15] => value15
    [16] => value16
    [17] => value17
    [18] => value18
    [19] => value19
    [20] => value20
    [21] => value21
    [22] => value22
    [23] => value23
    [24] => value24
    [25] => value25
    [26] => value26
)
but I need this:
Code:
Array
(
    [0] => Array
        (
	    [0] => Value0
	    [1] => value1
	    [2] => value2
	    [3] => value3
	    [4] => value4
	    [5] => value5
	    [6] => value6
	    [7] => value7
	    [8] => value8
        )

    [1] => Array
        (
	    [0] => value9
	    [1] => value10
	    [2] => value11
	    [3] => value12
	    [4] => value13
	    [5] => value14
	    [6] => value15
	    [7] => value16
	    [8] => value17
        )

    [2] => Array
        (
	    [0] => value18
	    [1] => value19
	    [2] => value20
	    [3] => value21
	    [4] => value22
	    [5] => value23
	    [6] => value24
	    [7] => value25
	    [8] => value26
        )
)
What is the most efficient way forward. I'm using using PHP 5.2

Thanks.
MattyUK is offline   Reply With Quote
Old 02-11-2010, 03:34 AM   PM User | #2
JAY6390
Regular Coder

 
Join Date: Dec 2009
Location: UK
Posts: 495
Thanks: 0
Thanked 58 Times in 58 Posts
JAY6390 is on a distinguished road
array_chunk()
PHP Code:
$output_array array_chunk($input_array9);
echo 
'<pre>'.print_r($output_arraytrue).'</pre>'
__________________
My site: JayGilford.com
Resources:
PHP Pagination Class | Getting all page links | Handling PHP Errors properly
If you like a users help, show your appreciation with the rep and thanks buttons :)
JAY6390 is offline   Reply With Quote
Old 02-11-2010, 04:01 AM   PM User | #3
MattyUK
Regular Coder

 
Join Date: Jul 2002
Posts: 301
Thanks: 7
Thanked 2 Times in 2 Posts
MattyUK is on a distinguished road
OO thanks. I hadn't stumbled across array_chunk before.
MattyUK is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:45 AM.


Advertisement
Log in to turn off these ads.