I have a function that is supposed to split my array into smaller, evenly distributed arrays, however it seems to be duplicating my data along the way. If anyone can help me out that'd be great.
我有一个函数,它应该将数组分割成更小的、均匀分布的数组,但是它似乎一直在复制我的数据。如果有人能帮我,那太好了。
Here's the original array:
这是原始数组:
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] =>
)
)
Here's the php function which is meant to split the array:
下面是用于分割数组的php函数:
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;
}
Here's the result of the split (it somehow duplicates items from the original array into the smaller arrays):
这是分割的结果(它以某种方式将原始数组中的项复制到较小的数组中):
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] =>
)
)
)
^^ As you can see there are duplicates from the original array in the newly created smaller arrays.
^ ^你可以看到有重复的原始数组的新创建的较小的数组。
I thought I could remove the duplicates using a multi-dimensional remove duplicate function but that didn't work. I'm guessing my problem is in the array_split function.
我想我可以用一个多维的删除重复的功能来删除重复的内容,但是那不起作用。我猜我的问题在array_split函数中。
Any suggestions? :)
有什么建议吗?:)
8 个解决方案
#1
7
EDIT: There's array_chunk, which does just that.
编辑:有array_chunk,它负责这个。
Well, I didn't feel like debugging, so I wrote a version with array_reduce:
我不喜欢调试,所以我用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()));
gives
给了
Array
(
[0] => Array
(
[0] => 1
[1] => 2
)
[1] => Array
(
[0] => 3
[1] => 4
)
[2] => Array
(
[0] => 5
)
)
#2
6
$sections = array_chunk(array('k', 'l', 'm', 'n', 'o'), 2);
#3
1
It seems to me that the distribution of the remaining items is too complicated.
在我看来,剩余项目的分配太复杂了。
If you know how many items are missing ($Remainder
), why don´t you just generate a remaining slice and pop-off items with array_pop()
until it´s empty?
如果你知道有多少物品丢失(剩余美元),为什么´t你只生成一个剩余与最后一片和大声讲话的物品(),直到´s空?
By the way, you can use that procedure for the whole array as well.
顺便说一下,你也可以对整个数组使用这个过程。
#4
1
function split_array(&$array, $slices) {
$result = array();
$l = count($array)-1;
for ($i=0; $i<=$l; $i++) {
if ($i == 0 || $i % $slices == 0) $tmp = array();
$tmp[] = $array[$i];
if ($i == $l || $i % $slices == 1) $result[] = $tmp;
}
return $result;
}
#5
1
There's array_chunk, which does just that.
array_chunk就是这样的。
http://www.php.net/manual/en/function.array-chunk.php
http://www.php.net/manual/en/function.array-chunk.php
[just making the salient part of Artefacto's answer more explicit]
[只是让这个事实的显著部分更明确地回答]
#6
1
array_chunk
doesn't fill the arrays evenly unless the total number of elements is divisible by the number of chunks you want; the last chunk may be much smaller than the first (e.g. if you have seven elements and you split in to three chunks, you'll get arrays containing three, three and one element).
array_chunk不能均匀填充数组,除非元素的总数可以被您想要的块数整除;最后一个块可能比第一个小得多(例如,如果你有7个元素,你分成3个块,你会得到包含3个、3个和1个元素的数组)。
The following implementation will attempt to smooth this out so that the array sizes are more even if that's what you're after, e.g. if you have seven elements you'll get chunks arrays containing three, two and two elements instead. It's still not even, but it's more even. It falls back to using array_chunk
if the count is evenly divisible by the number of columns, as that will be faster (especially if you have large arrays).
下面的实现将尝试平滑这一点,使数组大小更均匀,即使这是您所追求的,例如,如果您有7个元素,您将获得包含3、2和2个元素的块数组。它仍然不均匀,但更均匀。如果计数可以被列的数量均匀地整除,则返回使用array_chunk,因为这样会更快(特别是如果您有大型数组)。
<?php
function array_group($array, $num)
{
$num = (int) $num;
if ($num < 1) {
throw new \InvalidArgumentException('At least one group must be returned.');
}
$count = count($array);
if ($count && $count % $num === 0) {
return array_chunk($array, $count / $num);
}
$groups = [];
$offset = 0;
do {
$length = ceil(($count - $offset) / $num);
$groups[] = array_slice($array, $offset, $length);
$offset += $length;
} while (--$num);
return $groups;
}
print_r(array_chunk(array(1, 2, 3, 4, 5, 6, 7), 3));
/* Produces
Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[1] => Array
(
[0] => 4
[1] => 5
[2] => 6
)
[2] => Array
(
[0] => 7
)
) */
print_r(array_group(array(1, 2, 3, 4, 5, 6, 7), 3));
/* Produces
Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[1] => Array
(
[0] => 4
[1] => 5
)
[2] => Array
(
[0] => 6
[1] => 7
)
) */
#7
0
Try this a simple usage. When it finds empty string it splits the array into two arrays. One from beggining to empty string index. Other from empty string index to the last.
Note : Emty string is not included in both. it is used only for condition checking.
试试这个简单的用法。当它发现空字符串时,它将数组分成两个数组。从赤贫到空字符串索引。从空字符串索引到最后一个。注意:Emty字符串不包含在这两个字符串中。它只用于条件检查。
$column[] = "id";
$column[] = "name";
$column[] = "email";
$column[] = "password";
$column[] = "";
$column[] = "uid";
$column[] = "uname";
$column[] = "mname";
$column[] = "lname";
$column[] = "city";
$column[] = "country";
$column[] = "zip";
$column[] = "cell";
$column[] = "address";
split_array($column);
function split_array($column)
{
$total = count($column);
$num = array_search('',$column);
$split = $total - $num ;
$outer_sql = array_slice( $column , - ($split) + 1);
array_splice($column , $num);
echo "<pre>";
print_r($outer_sql);
echo "</pre>";
echo "<pre>";
print_r($column);
echo "</pre>";
}
#8
0
It is simple way to divide php array in two equal sections. and you can fetch all elements and values of both arrays using foreach easity
将php数组分成两个相等的部分是一种简单的方法。您可以使用foreach easy获取两个数组的所有元素和值
list($firstarray, $secondarray) = array_chunk($vorstand_two_column, ceil(count($all_array_contents) / 2));
foreach($firstarray as $fa) {
.... Code ....
}
foreach($secondarray as $sa) {
.... Code ....
}
#1
7
EDIT: There's array_chunk, which does just that.
编辑:有array_chunk,它负责这个。
Well, I didn't feel like debugging, so I wrote a version with array_reduce:
我不喜欢调试,所以我用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()));
gives
给了
Array
(
[0] => Array
(
[0] => 1
[1] => 2
)
[1] => Array
(
[0] => 3
[1] => 4
)
[2] => Array
(
[0] => 5
)
)
#2
6
$sections = array_chunk(array('k', 'l', 'm', 'n', 'o'), 2);
#3
1
It seems to me that the distribution of the remaining items is too complicated.
在我看来,剩余项目的分配太复杂了。
If you know how many items are missing ($Remainder
), why don´t you just generate a remaining slice and pop-off items with array_pop()
until it´s empty?
如果你知道有多少物品丢失(剩余美元),为什么´t你只生成一个剩余与最后一片和大声讲话的物品(),直到´s空?
By the way, you can use that procedure for the whole array as well.
顺便说一下,你也可以对整个数组使用这个过程。
#4
1
function split_array(&$array, $slices) {
$result = array();
$l = count($array)-1;
for ($i=0; $i<=$l; $i++) {
if ($i == 0 || $i % $slices == 0) $tmp = array();
$tmp[] = $array[$i];
if ($i == $l || $i % $slices == 1) $result[] = $tmp;
}
return $result;
}
#5
1
There's array_chunk, which does just that.
array_chunk就是这样的。
http://www.php.net/manual/en/function.array-chunk.php
http://www.php.net/manual/en/function.array-chunk.php
[just making the salient part of Artefacto's answer more explicit]
[只是让这个事实的显著部分更明确地回答]
#6
1
array_chunk
doesn't fill the arrays evenly unless the total number of elements is divisible by the number of chunks you want; the last chunk may be much smaller than the first (e.g. if you have seven elements and you split in to three chunks, you'll get arrays containing three, three and one element).
array_chunk不能均匀填充数组,除非元素的总数可以被您想要的块数整除;最后一个块可能比第一个小得多(例如,如果你有7个元素,你分成3个块,你会得到包含3个、3个和1个元素的数组)。
The following implementation will attempt to smooth this out so that the array sizes are more even if that's what you're after, e.g. if you have seven elements you'll get chunks arrays containing three, two and two elements instead. It's still not even, but it's more even. It falls back to using array_chunk
if the count is evenly divisible by the number of columns, as that will be faster (especially if you have large arrays).
下面的实现将尝试平滑这一点,使数组大小更均匀,即使这是您所追求的,例如,如果您有7个元素,您将获得包含3、2和2个元素的块数组。它仍然不均匀,但更均匀。如果计数可以被列的数量均匀地整除,则返回使用array_chunk,因为这样会更快(特别是如果您有大型数组)。
<?php
function array_group($array, $num)
{
$num = (int) $num;
if ($num < 1) {
throw new \InvalidArgumentException('At least one group must be returned.');
}
$count = count($array);
if ($count && $count % $num === 0) {
return array_chunk($array, $count / $num);
}
$groups = [];
$offset = 0;
do {
$length = ceil(($count - $offset) / $num);
$groups[] = array_slice($array, $offset, $length);
$offset += $length;
} while (--$num);
return $groups;
}
print_r(array_chunk(array(1, 2, 3, 4, 5, 6, 7), 3));
/* Produces
Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[1] => Array
(
[0] => 4
[1] => 5
[2] => 6
)
[2] => Array
(
[0] => 7
)
) */
print_r(array_group(array(1, 2, 3, 4, 5, 6, 7), 3));
/* Produces
Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[1] => Array
(
[0] => 4
[1] => 5
)
[2] => Array
(
[0] => 6
[1] => 7
)
) */
#7
0
Try this a simple usage. When it finds empty string it splits the array into two arrays. One from beggining to empty string index. Other from empty string index to the last.
Note : Emty string is not included in both. it is used only for condition checking.
试试这个简单的用法。当它发现空字符串时,它将数组分成两个数组。从赤贫到空字符串索引。从空字符串索引到最后一个。注意:Emty字符串不包含在这两个字符串中。它只用于条件检查。
$column[] = "id";
$column[] = "name";
$column[] = "email";
$column[] = "password";
$column[] = "";
$column[] = "uid";
$column[] = "uname";
$column[] = "mname";
$column[] = "lname";
$column[] = "city";
$column[] = "country";
$column[] = "zip";
$column[] = "cell";
$column[] = "address";
split_array($column);
function split_array($column)
{
$total = count($column);
$num = array_search('',$column);
$split = $total - $num ;
$outer_sql = array_slice( $column , - ($split) + 1);
array_splice($column , $num);
echo "<pre>";
print_r($outer_sql);
echo "</pre>";
echo "<pre>";
print_r($column);
echo "</pre>";
}
#8
0
It is simple way to divide php array in two equal sections. and you can fetch all elements and values of both arrays using foreach easity
将php数组分成两个相等的部分是一种简单的方法。您可以使用foreach easy获取两个数组的所有元素和值
list($firstarray, $secondarray) = array_chunk($vorstand_two_column, ceil(count($all_array_contents) / 2));
foreach($firstarray as $fa) {
.... Code ....
}
foreach($secondarray as $sa) {
.... Code ....
}