views:

328

answers:

2

Hi all, I have a mysql query which spits out the following:

Array
(
    [0] => stdClass Object
        (
            [bid] => 18
            [name] => Cafe Domingo
            [imageurl] => sp_domingo.gif
            [clickurl] => #
        )

    [1] => stdClass Object
        (
            [bid] => 19
            [name] => Industrial Sweeping Services
            [imageurl] => sp_iss.gif
            [clickurl] => http://www.industrialsweeping.com.au/
        )

    [2] => stdClass Object
        (
            [bid] => 22
            [name] => Melbourne Food Distributors
            [imageurl] => sp_mfd.gif
            [clickurl] => http://www.campbells.com.au/
        )

    [3] => stdClass Object
        (
            [bid] => 26
            [name] => Toyota Chadstone
            [imageurl] => sp_toyota.jpg
            [clickurl] => http://www.chadstonetoyota.com.au/
        )

    [4] => stdClass Object
        (
            [bid] => 15
            [name] => Bay Corporate Catering
            [imageurl] => sp_baycorp.gif
            [clickurl] => http://www.baycorporatecatering.com.au/
        )

    [5] => stdClass Object
        (
            [bid] => 24
            [name] => Steve Wilby Transport
            [imageurl] => sp_swilky.gif
            [clickurl] => http://www.stevewilby.com.au/
        )

    [6] => stdClass Object
        (
            [bid] => 17
            [name] => Cody Gems and Jewellery
            [imageurl] => sp_cody.gif
            [clickurl] => #
        )

    [7] => stdClass Object
        (
            [bid] => 21
            [name] => Matthew Davis Australia Pty Ltd
            [imageurl] => sp_matthewdavis.gif
            [clickurl] => http://www.matthewdavis.com.au/
        )

    [8] => stdClass Object
        (
            [bid] => 25
            [name] => Tom the Lumberjack
            [imageurl] => sp_tom.gif
            [clickurl] => http://www.redwoodgardens.com.au/
        )

    [9] => stdClass Object
        (
            [bid] => 16
            [name] => Bendigo Bank
            [imageurl] => sp_bb.gif
            [clickurl] => http://www.bendigobank.com.au/
        )

    [10] => stdClass Object
        (
            [bid] => 14
            [name] => 360South Pty Ltd
            [imageurl] => sp_360south.gif
            [clickurl] => http://www.360south.com.au/
        )

    [11] => stdClass Object
        (
            [bid] => 23
            [name] => Redwood Gardens Chinese Restaurant
            [imageurl] => sp_redwood.gif
            [clickurl] => http://www.redwoodgardens.com.au/
        )

)

Is it at all possible to then split that array into 4 arrays? With an equal amount of items in each array (as best as possible) but able to be dynamic as more information is put into the dbase).. So if there's an uneven amount, the last array has less?

If that makes sense?

+2  A: 

Sure you can! The following code will do just that, split the result into the numbers of slices you specify.

function split_array($array, $slices) {
  $perSlice = floor(count($array) / $slices);
  $sliceExtra = count($array) % $slices;

  $slicesArray = array();
  $offset = 0;

  for($i = 0; $i < $slices; $i++) {
    $extra = (($sliceExtra--) > 0) ? 1 : 0;
    $slicesArray[] = array_slice($array, $offset, $perSlice + $extra);
    $offset += $perSlice + $extra;
  }

  return $slicesArray;
}

$slices = split_array($mysqlResult,4);

EDIT: Edited to make more even slices.

Andrew Moore
The last line is wrong: it should be $slices = split_array($mysqlResult, 4);
inakiabt
**@inakiabt:** I've modified my code. Thanks.
Andrew Moore
You stole my answer >:(
Peter Bailey
At least have the courtesy to give me a +1, sheesh.
Peter Bailey
**@Peter:** Unfortunately, I was in the process of editing my answer when you posted yours.
Andrew Moore
Wait... did you actually just downvote mine?
Peter Bailey
No, I gave you +1 the moment I saw your answer.
Andrew Moore
Ok, sorry. I didn't mean to be accusatory. Yesterday you edited your post 5 minutes after I posted mine, and today my answer here was downvoted right as you posted your comment above. Just bad timing I guess.
Peter Bailey
While fixing the error inakiabt posted yesterday, I realised that I could of used `array_chunk()` instead of doing it manually. I had to do a bit of research in the PHP docs as I couldn't remember the arguments. By the time I was done, you posted your answer.
Andrew Moore
Ok this code is splitting up my original array into:array1 = 4 itemsarray2 = 4 itemsarray3 = 4 itemsarray4 = 1 item^^ Is there any way to make that more even?
SoulieBaby
**@SoulieBaby:** I've just edited my answer to make more even slices.
Andrew Moore
+3  A: 

I think this works

$splitArray = array_chunk( $sourceArray, ceil( count( $sourceArray ) / 4 ) );
Peter Bailey