简体   繁体   中英

Split MYSQL results into 4 arrays

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?

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.

我认为这有效

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM