küçüğünü dahi diziler içine php bölünmüş dizi

3 Cevap php

Ben ancak yol boyunca benim veri çoğaltma gibi görünüyor, küçük, eşit dağıtılmış diziler içine benim dizi bölmek için beklenen bir işlevi var. Herkes bana yardımcı olabilir eğer bu harika olurdu.

Burada orijinal dizi var:

Array
(
    [0] => stdClass Object
        (
            [bid] => 42
            [name] => Ray White Mordialloc
            [imageurl] => sp_raywhite.gif
            [clickurl] => http://www.raywhite.com/
        )

    [1] => stdClass Object
        (
            [bid] => 48
            [name] => Beachside Osteo
            [imageurl] => sp_beachside.gif
            [clickurl] => http://www.beachsideosteo.com.au/
        )

    [2] => stdClass Object
        (
            [bid] => 53
            [name] => Carmotive
            [imageurl] => sp_carmotive.jpg
            [clickurl] => http://www.carmotive.com.au/
        )

    [3] => stdClass Object
        (
            [bid] => 51
            [name] => Richmond and Bennison
            [imageurl] => sp_richmond.jpg
            [clickurl] => http://www.richbenn.com.au/
        )

    [4] => stdClass Object
        (
            [bid] => 50
            [name] => Letec
            [imageurl] => sp_letec.jpg
            [clickurl] => www.letec.biz
        )

    [5] => stdClass Object
        (
            [bid] => 39
            [name] => Main Street Mordialloc
            [imageurl] => main street cafe.jpg
            [clickurl] => 
        )

    [6] => stdClass Object
        (
            [bid] => 40
            [name] => Ripponlea Mitsubishi
            [imageurl] => sp_mitsubishi.gif
            [clickurl] => 
        )

    [7] => stdClass Object
        (
            [bid] => 34
            [name] => Adrianos Pizza & Pasta
            [imageurl] => sp_adrian.gif
            [clickurl] => 
        )

    [8] => stdClass Object
        (
            [bid] => 59
            [name] => Pure Sport
            [imageurl] => sp_psport.jpg
            [clickurl] => http://www.puresport.com.au/
        )

    [9] => stdClass Object
        (
            [bid] => 33
            [name] => Two Brothers
            [imageurl] => sp_2brothers.gif
            [clickurl] => http://www.2brothers.com.au/
        )

    [10] => stdClass Object
        (
            [bid] => 52
            [name] => Mordialloc Travel and Cruise
            [imageurl] => sp_morditravel.jpg
            [clickurl] => http://www.yellowpages.com.au/vic/mordialloc/mordialloc-travel-cruise-13492525-listing.html
        )

    [11] => stdClass Object
        (
            [bid] => 57
            [name] => Southern Suburbs Physiotherapy Centre
            [imageurl] => sp_sspc.jpg
            [clickurl] => http://www.sspc.com.au
        )

    [12] => stdClass Object
        (
            [bid] => 54
            [name] => PPM Builders
            [imageurl] => sp_ppm.jpg
            [clickurl] => http://www.hotfrog.com.au/Companies/P-P-M-Builders
        )

    [13] => stdClass Object
        (
            [bid] => 36
            [name] => Big River
            [imageurl] => sp_bigriver.gif
            [clickurl] => 
        )

    [14] => stdClass Object
        (
            [bid] => 35
            [name] => Bendigo Bank Parkdale / Mentone East
            [imageurl] => sp_bendigo.gif
            [clickurl] => http://www.bendigobank.com.au
        )

    [15] => stdClass Object
        (
            [bid] => 56
            [name] => Logical Services
            [imageurl] => sp_logical.jpg
            [clickurl] => 
        )

    [16] => stdClass Object
        (
            [bid] => 58
            [name] => Dicount Lollie Shop
            [imageurl] => new dls logo.jpg
            [clickurl] => 
        )

    [17] => stdClass Object
        (
            [bid] => 46
            [name] => Patterson Securities
            [imageurl] => cmyk patersons_withtag.jpg
            [clickurl] => 
        )

    [18] => stdClass Object
        (
            [bid] => 44
            [name] => Mordialloc Personal Trainers
            [imageurl] => sp_mordipt.gif
            [clickurl] => #
        )

    [19] => stdClass Object
        (
            [bid] => 37
            [name] => Mordialloc Cellar Door
            [imageurl] => sp_cellardoor.gif
            [clickurl] => 
        )

    [20] => stdClass Object
        (
            [bid] => 41
            [name] => Print House Graphics
            [imageurl] => sp_printhouse.gif
            [clickurl] => 
        )

    [21] => stdClass Object
        (
            [bid] => 55
            [name] => 360South
            [imageurl] => sp_360.jpg
            [clickurl] => 
        )

    [22] => stdClass Object
        (
            [bid] => 43
            [name] => Systema
            [imageurl] => sp_systema.gif
            [clickurl] => 
        )

    [23] => stdClass Object
        (
            [bid] => 38
            [name] => Lowe Financial Group
            [imageurl] => sp_lowe.gif
            [clickurl] => http://lowefinancial.com/
        )

    [24] => stdClass Object
        (
            [bid] => 49
            [name] => Kim Reed Conveyancing
            [imageurl] => sp_kimreed.jpg
            [clickurl] => 
        )

    [25] => stdClass Object
        (
            [bid] => 45
            [name] => Mordialloc Sporting Club
            [imageurl] => msc logo.jpg
            [clickurl] => 
        )

)

İşte dizi bölmek içindir php fonksiyonu bulunuyor:

function split_array($array, $slices) { 

        $perGroup = floor(count($array) / $slices);
        $Remainder = count($array) % $slices ;
        $slicesArray = array();

        $i = 0;
        while( $i < $slices ) {
            $slicesArray[$i] = array_slice($array, $i * $perGroup, $perGroup);
            $i++;
        }

        if ( $i == $slices ) { 
            if ($Remainder > 0 && $Remainder < $slices) {

                $z = $i * $perGroup +1;
                $x = 0;
                while ($x < $Remainder) {

                    $slicesRemainderArray = array_slice($array, $z, $Remainder+$x);
                    $remainderItems = array_merge($slicesArray[$x],$slicesRemainderArray);
                    $slicesArray[$x] = $remainderItems;

                $x++;
                $z++;
                }
            }
        };

        return $slicesArray;
    }

Burada bölünmüş (bu şekilde daha küçük diziler halinde, orijinal dizideki öğeleri yineler) bir sonucudur:

Array
(
    [0] => Array
        (
            [0] => stdClass Object
                (
                    [bid] => 57
                    [name] => Southern Suburbs Physiotherapy Centre
                    [imageurl] => sp_sspc.jpg
                    [clickurl] => http://www.sspc.com.au
                )

            [1] => stdClass Object
                (
                    [bid] => 35
                    [name] => Bendigo Bank Parkdale / Mentone East
                    [imageurl] => sp_bendigo.gif
                    [clickurl] => http://www.bendigobank.com.au
                )

            [2] => stdClass Object
                (
                    [bid] => 38
                    [name] => Lowe Financial Group
                    [imageurl] => sp_lowe.gif
                    [clickurl] => http://lowefinancial.com/
                )

            [3] => stdClass Object
                (
                    [bid] => 39
                    [name] => Main Street Mordialloc
                    [imageurl] => main street cafe.jpg
                    [clickurl] => 
                )

            [4] => stdClass Object
                (
                    [bid] => 48
                    [name] => Beachside Osteo
                    [imageurl] => sp_beachside.gif
                    [clickurl] => http://www.beachsideosteo.com.au/
                )

            [5] => stdClass Object
                (
                    [bid] => 33
                    [name] => Two Brothers
                    [imageurl] => sp_2brothers.gif
                    [clickurl] => http://www.2brothers.com.au/
                )

            [6] => stdClass Object
                (
                    [bid] => 40
                    [name] => Ripponlea Mitsubishi
                    [imageurl] => sp_mitsubishi.gif
                    [clickurl] => 
                )

        )

    [1] => Array
        (
            [0] => stdClass Object
                (
                    [bid] => 44
                    [name] => Mordialloc Personal Trainers
                    [imageurl] => sp_mordipt.gif
                    [clickurl] => #
                )

            [1] => stdClass Object
                (
                    [bid] => 41
                    [name] => Print House Graphics
                    [imageurl] => sp_printhouse.gif
                    [clickurl] => 
                )

            [2] => stdClass Object
                (
                    [bid] => 39
                    [name] => Main Street Mordialloc
                    [imageurl] => main street cafe.jpg
                    [clickurl] => 
                )

            [3] => stdClass Object
                (
                    [bid] => 48
                    [name] => Beachside Osteo
                    [imageurl] => sp_beachside.gif
                    [clickurl] => http://www.beachsideosteo.com.au/
                )

            [4] => stdClass Object
                (
                    [bid] => 33
                    [name] => Two Brothers
                    [imageurl] => sp_2brothers.gif
                    [clickurl] => http://www.2brothers.com.au/
                )

            [5] => stdClass Object
                (
                    [bid] => 40
                    [name] => Ripponlea Mitsubishi
                    [imageurl] => sp_mitsubishi.gif
                    [clickurl] => 
                )

        )

    [2] => Array
        (
            [0] => stdClass Object
                (
                    [bid] => 56
                    [name] => Logical Services
                    [imageurl] => sp_logical.jpg
                    [clickurl] => 
                )

            [1] => stdClass Object
                (
                    [bid] => 43
                    [name] => Systema
                    [imageurl] => sp_systema.gif
                    [clickurl] => 
                )

            [2] => stdClass Object
                (
                    [bid] => 48
                    [name] => Beachside Osteo
                    [imageurl] => sp_beachside.gif
                    [clickurl] => http://www.beachsideosteo.com.au/
                )

            [3] => stdClass Object
                (
                    [bid] => 33
                    [name] => Two Brothers
                    [imageurl] => sp_2brothers.gif
                    [clickurl] => http://www.2brothers.com.au/
                )

            [4] => stdClass Object
                (
                    [bid] => 40
                    [name] => Ripponlea Mitsubishi
                    [imageurl] => sp_mitsubishi.gif
                    [clickurl] => 
                )

        )

    [3] => Array
        (
            [0] => stdClass Object
                (
                    [bid] => 53
                    [name] => Carmotive
                    [imageurl] => sp_carmotive.jpg
                    [clickurl] => http://www.carmotive.com.au/
                )

            [1] => stdClass Object
                (
                    [bid] => 45
                    [name] => Mordialloc Sporting Club
                    [imageurl] => msc logo.jpg
                    [clickurl] => 
                )

            [2] => stdClass Object
                (
                    [bid] => 33
                    [name] => Two Brothers
                    [imageurl] => sp_2brothers.gif
                    [clickurl] => http://www.2brothers.com.au/
                )

            [3] => stdClass Object
                (
                    [bid] => 40
                    [name] => Ripponlea Mitsubishi
                    [imageurl] => sp_mitsubishi.gif
                    [clickurl] => 
                )

        )

    [4] => Array
        (
            [0] => stdClass Object
                (
                    [bid] => 59
                    [name] => Pure Sport
                    [imageurl] => sp_psport.jpg
                    [clickurl] => http://www.puresport.com.au/
                )

            [1] => stdClass Object
                (
                    [bid] => 54
                    [name] => PPM Builders
                    [imageurl] => sp_ppm.jpg
                    [clickurl] => http://www.hotfrog.com.au/Companies/P-P-M-Builders
                )

            [2] => stdClass Object
                (
                    [bid] => 40
                    [name] => Ripponlea Mitsubishi
                    [imageurl] => sp_mitsubishi.gif
                    [clickurl] => 
                )

        )

    [5] => Array
        (
            [0] => stdClass Object
                (
                    [bid] => 46
                    [name] => Patterson Securities
                    [imageurl] => cmyk patersons_withtag.jpg
                    [clickurl] => 
                )

            [1] => stdClass Object
                (
                    [bid] => 34
                    [name] => Adriano's Pizza & Pasta
                    [imageurl] => sp_adrian.gif
                    [clickurl] => #
                )

        )

    [6] => Array
        (
            [0] => stdClass Object
                (
                    [bid] => 55
                    [name] => 360South
                    [imageurl] => sp_360.jpg
                    [clickurl] => 
                )

            [1] => stdClass Object
                (
                    [bid] => 37
                    [name] => Mordialloc Cellar Door
                    [imageurl] => sp_cellardoor.gif
                    [clickurl] => 
                )

        )

    [7] => Array
        (
            [0] => stdClass Object
                (
                    [bid] => 49
                    [name] => Kim Reed Conveyancing
                    [imageurl] => sp_kimreed.jpg
                    [clickurl] => 
                )

            [1] => stdClass Object
                (
                    [bid] => 58
                    [name] => Dicount Lollie Shop
                    [imageurl] => new dls logo.jpg
                    [clickurl] => 
                )

        )

    [8] => Array
        (
            [0] => stdClass Object
                (
                    [bid] => 51
                    [name] => Richmond and Bennison
                    [imageurl] => sp_richmond.jpg
                    [clickurl] => http://www.richbenn.com.au/
                )

            [1] => stdClass Object
                (
                    [bid] => 52
                    [name] => Mordialloc Travel and Cruise
                    [imageurl] => sp_morditravel.jpg
                    [clickurl] => http://www.yellowpages.com.au/vic/mordialloc/mordialloc-travel-cruise-13492525-listing.html
                )

        )

    [9] => Array
        (
            [0] => stdClass Object
                (
                    [bid] => 50
                    [name] => Letec
                    [imageurl] => sp_letec.jpg
                    [clickurl] => www.letec.biz
                )

            [1] => stdClass Object
                (
                    [bid] => 36
                    [name] => Big River
                    [imageurl] => sp_bigriver.gif
                    [clickurl] => 
                )

        )

)

^ ^ Gördüğünüz gibi yeni oluşturulan küçük diziler orijinal dizideki çiftleri vardır.

Ben çok boyutlu kaldır yinelenen fonksiyonunu kullanarak çiftleri kaldırmak olabilir düşündüm ama işe yaramadı. Benim sorunum array_split işlevi olduğunu tahmin ediyorum.

Herhangi bir öneriniz? :)

3 Cevap

EDIT: yok sadece, hangi array_chunk var.

Eh, ben hata ayıklama gibi hissediyorum vermedi, bu yüzden bir versiyonunu yazdı array_reduce:

$pergroup = 2;
$redfunc = function ($partial, $elem) use ($pergroup) {
    $groupCount = count($partial);
    if ($groupCount == 0 || count(end($partial)) == $pergroup)
        $partial[] = array($elem);
    else
        $partial[$groupCount-1][] = $elem;

    return $partial;
};

$arr = array(1,2,3,4,5);

print_r(array_reduce($arr, $redfunc, array()));

verir

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 2
        )

    [1] => Array
        (
            [0] => 3
            [1] => 4
        )

    [2] => Array
        (
            [0] => 5
        )

)

Bu kalan kalemlerin dağılımı çok karmaşık geliyor bana.

Kaç ürün eksik biliyorsanız boşalıncaya kadar ($Remainder), neden sadece bir kalan dilim oluşturmak ve array_pop() ile pop-off ürün değil mi?

Bu arada, siz de tüm dizi için bu yordamı kullanabilirsiniz.

Yok sadece hangi array_chunk var.

http://www.php.net/manual/en/function.array-chunk.php

[Sadece artefacto cevabı göze çarpan kısmı daha açık hale]