Laravel Collection push()没有正常工作 [英] Laravel Collection push() not properly working

查看:171
本文介绍了Laravel Collection push()没有正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图根据日期范围复制集合中的项目。例如,我有一个这样的JSON:

  {
title:200,
开始:2017-12-20,
endx:2017-12-25,
设备:椅子,
数量:200

$ / code>

现在我想复制6x,因为从12-20到6天有6天12-25。像这样:

  {
title:200,
start:2017 -12-20,
endx:2017-12-20,
设备:椅子,
数量:200
}

{
title:200,
start:2017-12-21,
endx:2017-12-21 ,
设备:椅子,
数量:200
}

{
标题:200,
开始:2017-12-22,
endx:2017-12-22,
设备:椅子,
数量: 200
}

{
title:200,
start:2017-12-23,
endx :2017-12-23,
设备:椅子,
数量:200
}

{
title :200,
开始:2017-12-24,
endx:2017-12-24,
设备:椅子,
quantity:200
}

{
title:200,
start:2017-12-25 ,
endx:2017-12-25,
equipment:椅子,
数量:200
}

我想这是因为我之前的问题的答案:<基于日期的FullCalendar基础上重复事件开始和结束



以下是我的代码:

  $ events = Event :: all(); 

foreach($ events as $ i => $ event){
if($ event-> start!= $ event-> endx)
{
$ date = $ event-> start;
$ end = $ event-> endx; (strtotime($ date)< = strtotime($ end)){
$ new = [];

$ new = $ event;

$ new-> start = $ date;
$ new-> endx = $ date;

$ events-> push($ new);

$ date = date(Y-m-d,strtotime(+ 1 day,strtotime($ date)));
}
}
}

return $ events-> all();

但是我得到的唯一结果是它基于最后一个日期重复6次2017-12- 25\" 。像这样:

  {
title:200,
start:2017 -12-25,
endx:2017-12-25,
equipment:椅子,
数量:200
}

{
title:200,
start:2017-12-25,
endx:2017-12-25 ,
设备:椅子,
数量:200
}

{
标题:200,
开始:2017-12-25,
endx:2017-12-25,
设备:椅子,
数量: 200
}

{
title:200,
start:2017-12-25,
endx :2017-12-25,
装备:椅子,
数量:200
}

{
title :200,
开始:2017-12-25,
endx:2017-12-25,
设备:椅子,
quantity:200
}

{
title:200,
start:2017-12-25 ,
endx:2017-12-25,
equipment:椅子,
数量:200
}


解决方案

问题是:

$ new 是一个对象。您推送到该集合的 $ new 与您将在下一个将使用的 $ new 相同迭代。为了进一步描述情况:

  $ a = new stdClass; 
$ a-> name =foo;

$ myArray = [$ a];

var_dump($ myArray);
// array(1){
// [0] =>
// object(stdClass)#1(1){
// [name] =>
// string(3)foo
//}
//}

$ a-> name =bar;
var_dump($ myArray);
// array(1){
// [0] =>
// object(stdClass)#1(1){
// [name] =>
// string(3)bar
//}
//}

$ new 通过引用传递给 push 方法,或者某些人会说,被赋值

这就是你的情况所发生的魔法,为了解决这个问题,你应该推送一个克隆的对象 $ new

您可以在这里跟踪 push 代码: https://github.com/laravel/framework/blob/5.5/src/Illuminate /Support/Collection.php#L1129



更改 $ events-> push($ new); to $ events-> push(clone $ new);


I am trying to duplicate an item in a collection based on the date range. For example, I have a JSON of this:

{
"title": " 200",
"start": "2017-12-20",
"endx": "2017-12-25",
"equipment": "Chairs",
"quantity": 200
}

Now I want to duplicate 6x because there are 6 days from 12-20 to 12-25. Like this:

{
"title": " 200",
"start": "2017-12-20",
"endx": "2017-12-20",
"equipment": "Chairs",
"quantity": 200
}

{
"title": " 200",
"start": "2017-12-21",
"endx": "2017-12-21",
"equipment": "Chairs",
"quantity": 200
}

{
"title": " 200",
"start": "2017-12-22",
"endx": "2017-12-22",
"equipment": "Chairs",
"quantity": 200
}

{
"title": " 200",
"start": "2017-12-23",
"endx": "2017-12-23",
"equipment": "Chairs",
"quantity": 200
}

{
"title": " 200",
"start": "2017-12-24",
"endx": "2017-12-24",
"equipment": "Chairs",
"quantity": 200
}

{
"title": " 200",
"start": "2017-12-25",
"endx": "2017-12-25",
"equipment": "Chairs",
"quantity": 200
}

I want this because of the answer from my previous question: Repeat events on FullCalendar base on date start and end

Here is my code:

$events = Event::all();

        foreach ($events as $i => $event) {
            if($event->start != $event->endx)
            {
                $date = $event->start;
                $end = $event->endx;
                while (strtotime($date) <= strtotime($end)) {
                    $new = [];
                    $new = $event;

                    $new->start = $date;
                    $new->endx = $date;

                    $events->push($new);

                    $date = date ("Y-m-d", strtotime("+1 day", strtotime($date)));
                }
            }
        }

        return $events->all();

But the only output I got is that it repeats 6x based on the last date "2017-12-25". Like this:

{
"title": " 200",
"start": "2017-12-25",
"endx": "2017-12-25",
"equipment": "Chairs",
"quantity": 200
}

{
"title": " 200",
"start": "2017-12-25",
"endx": "2017-12-25",
"equipment": "Chairs",
"quantity": 200
}

{
"title": " 200",
"start": "2017-12-25",
"endx": "2017-12-25",
"equipment": "Chairs",
"quantity": 200
}

{
"title": " 200",
"start": "2017-12-25",
"endx": "2017-12-25",
"equipment": "Chairs",
"quantity": 200
}

{
"title": " 200",
"start": "2017-12-25",
"endx": "2017-12-25",
"equipment": "Chairs",
"quantity": 200
}

{
"title": " 200",
"start": "2017-12-25",
"endx": "2017-12-25",
"equipment": "Chairs",
"quantity": 200
}

解决方案

The problem is:

$new is an object. The $new that you've pushed to that collection is the same $new that you will be using on the next iteration. To further describe the situation:

$a = new stdClass;
$a->name = "foo";

$myArray = [$a];

var_dump($myArray);
// array(1) {
//  [0]=>
//  object(stdClass)#1 (1) {
//    ["name"]=>
//    string(3) "foo"
//  }
//}

$a->name = "bar";
var_dump($myArray);
// array(1) {
//  [0]=>
//  object(stdClass)#1 (1) {
//    ["name"]=>
//    string(3) "bar"
//  }
//}

$new is being passed by reference to the push method, or some would say, assigned by reference.

That's the magic that is happening in your case, to fix that, you should push a cloned object of $new.

You can trace the push code here: https://github.com/laravel/framework/blob/5.5/src/Illuminate/Support/Collection.php#L1129

Change $events->push($new); to $events->push(clone $new);

这篇关于Laravel Collection push()没有正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆