laravel PHP:如何将数组分成两部分?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25540355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP: How to split array into 2 parts?
提问by KriiV
I have an array full of DB records. It could heaps of elements or very little, it changes regularly.
我有一个充满数据库记录的数组。它可能有很多元素,也可能很少,它会定期变化。
I need to split the array into two equal parts. The reason is I am then passing these arrays to a Laravel view and displaying them into separate columns.
我需要将数组分成两个相等的部分。原因是我然后将这些数组传递给 Laravel 视图并将它们显示到单独的列中。
Here is the DB records being pulled:
这是正在提取的数据库记录:
$books = Book::where('unitcode', '=', $unitcode['unitcode'])->get();
$books = Book::where('unitcode', '=', $unitcode['unitcode'])->get();
If the pull works then I'm running this:
如果拉动工作,那么我正在运行这个:
return View::make('showbooks')
->with('books', $books);
What I want to do is pass $books1
and $books2
which is actually $books
split into 2 parts.
我想做的是通过$books1
,$books2
实际上$books
分为两部分。
Any ideas on how I could do this? Thanks
关于我如何做到这一点的任何想法?谢谢
回答by Ohgodwhy
This is a one liner:
这是一个单班轮:
$halved = array_chunk($books, ceil(count($books)/2));
Then $halved[0]
will contain the first half of the array. It will always be 1 element larger in the event that the array contains an odd number of elements. Of course, $halved[1]
will contain the 2nd half of the array.
然后$halved[0]
将包含数组的前半部分。如果数组包含奇数个元素,它将始终大 1 个元素。当然,$halved[1]
将包含数组的第二部分。
回答by Mad Angle
I think this will work for you.
我认为这对你有用。
<?php
$books = array("a", "b", "c", "d", "e","f"); // assuming books is an array like this
$count = count($books); // total count of the array books
$half = $count/2; // half of the total count
$books1 = array_slice($books, 0, $half); // returns first half
$books2 = array_slice($books, $half); // returns second half
print_r($books1);
print_r($books2);
?>
回答by Abdes sabor
I think you should take the length of the array using the count method and then loop the items till the middle it should look like this :
我认为你应该使用 count 方法获取数组的长度,然后循环项目直到中间它应该是这样的:
<?php
$count = count($books); //getting the count of $books
$mid = round($count/2); //getting the mid point of $books
for($i = 0; $i < $count ; $i++){ //looping through the indexes
if($i <= mid - 1){ //Checking the position of $i and adding the value
$books1[] = $books[$i];
}else{
$books2[] = $books[$i];
}
}
?>
回答by Jerre
To expand on Ohgodwhy's answer:
扩展Ohgodwhy的回答:
Since you're using laravel, here's a more fluent way to achieve this:
由于您使用的是laravel,这里有一种更流畅的方法来实现这一点:
$books = collect($books);
$halved = $books->split(ceil($books->count()/2))->toArray();
// Result: [[a, b, c], [d, e, f]]
Take a look at Laravel's various Collection Methodsfor more available methods to fluently manipulate arrays.
查看 Laravel 的各种Collection Methods以获取更多可用的方法来流畅地操作数组。