【问题标题】:Merge two Laravel collections elements and replace合并两个 Laravel 集合元素并替换
【发布时间】:2016-12-19 19:52:23
【问题描述】:

我有一个名为 $box_items 的 laravel 集合。

Collection {#320 ▼
  #items: array:3 [▼
    0 => array:6 [▼
      "img_alt" => "<span>Original</span> gifts"
      "class" => "personalised-gifts"
      "elements" => array:2 [▼
        0 => array:2 [▼
          "id" => 2638
          "type" => "ARTICLE"
        ]
        1 => array:2 [▼
          "id" => 2100
          "type" => "ARTICLE"
        ]
      ]
    ]
    1 => array:5 [▼
      "img_alt" => "<span>Love</span> gifts"
      "class" => "love-gifts"
      "elements" => array:3 [▼
        0 => array:2 [▼
          "id" => 1072
          "type" => "CATEGORY"
        ]
        1 => array:2 [▼
          "id" => 6186
          "type" => "ARTICLE"
        ]
        2 => array:2 [▼
          "id" => 1028
          "type" => "CATEGORY"
        ]
      ]
    ]

另一方面,我有另一个名为 $elements 的集合,这个 $elements 集合有关于 $box_items 中的“元素”字段的额外信息 收藏。

这是我的 $elements 收藏:

array:5 [▼
   0 => {#313 ▼
      +"type": "ARTICLE"
      +"name": "Ceramic Mug"
      +"resource_id": "2638"
      +"seo": {#314 ▶}
  }

  1 => {#323 ▼
    +"type": "CATEGORY"
    +"name": "Personalised Blankets"
    +"category": {#325 ▼
      +"id": 1072
      +"gallery_id": null
      +"final": true
    }
    +"seo": {#326 ▶}
  }

  2 => {#327 ▼
      +"type": "ARTICLE"
      +"name": "Circle Cushion"
      +"resource_id": "2100"
      +"seo": {#328 ▶}
  }

  3 => {#329 ▼
      +"type": "ARTICLE"
      +"name": "Book"
      +"resource_id": "6186"
      +"seo": {#330 ▶}
  }

  4 => {#341 ▼
    +"type": "CATEGORY"
    +"name": "Gifts for men"
    +"category": {#342 ▼
      +"id": 1028
      +"gallery_id": null
      +"final": false
    }
    +"seo": {#343 ▶}
  }

]

我想用 $box_items 元素字段替换 $elements CATEGORIES 和 ARTICLES。

我想要这个最终结果:

Collection {#320 ▼
  #items: array:3 [▼
    0 => array:6 [▼
      "img_alt" => "<span>Original</span> gifts"
      "class" => "personalised-gifts"
      "elements" => array:2 [▼
        0 => {#313 ▼
            +"type": "ARTICLE"
            +"name": "Ceramic Mug"
            +"resource_id": "2638"
            +"seo": {#314 ▶}
        }
        2 => {#327 ▼
            +"type": "ARTICLE"
            +"name": "Circle Cushion"
            +"resource_id": "2100"
            +"seo": {#328 ▶}
        }
      ]
    ]
    1 => array:5 [▼
      "img_alt" => "<span>Love</span> gifts"
      "class" => "love-gifts"
      "elements" => array:3 [▼
         0 => {#323 ▼
          +"type": "CATEGORY"
          +"name": "Personalised Blankets"
          +"category": {#325 ▼
            +"id": 1072
            +"gallery_id": null
            +"final": true
          }
          +"seo": {#326 ▶}
        }
        1 => {#329 ▼
            +"type": "ARTICLE"
            +"name": "Book"
            +"resource_id": "6186"
            +"seo": {#330 ▶}
        }
        2 => {#341 ▼
          +"type": "CATEGORY"
          +"name": "Gifts for men"
          +"category": {#342 ▼
            +"id": 1028
            +"gallery_id": null
            +"final": false
          }
          +"seo": {#343 ▶}
        }
      ]
    ]

编辑:if($article_element['id'] == $extra_element->category->id); 行中的错误;

foreach($articles_rows as &$article)
        {

           foreach($article['elements'] as &$article_element)
           {

              foreach($cats_and_arts as $extra_element)
              {

                if($extra_element->type == 'CATEGORY')
                {

                    if($article_element['id'] == $extra_element->category->id)//Undefined index: id
                    {

                         $article_element = (array)$extra_element;
                    }
                }

                if($extra_element->type == 'ARTICLE')
                {

                    if($article_element['id'] == $extra_element->article->id)
                    {

                         $article_element = (array)$extra_element;

                    }
                }

              }
           }
         }
            dd($articles_rows);

【问题讨论】:

    标签: php laravel laravel-5 laravel-5.2 laravel-5.3


    【解决方案1】:

    试试这个。它应该适合你。

    foreach($box_items as &$box){
       foreach($box["elements"] as &$box_element){
          foreach($elements as $element){
             if($box_element->id == $element->id){
                $box_element = $element;
             }
          }
       }
     }
    

    【讨论】:

    • 但是我在哪里可以存储所有信息?,比如最终结果
    • 它将在 $box_items 中可用,项目由引用传递 dd($box_items) 以查看更新
    • 好的,这会返回这个错误:“尝试修改非对象的属性”,因为字段元素包含数组而不是对象...
    • 请问什么变量被引用为非对象
    • 在 foreach($box->elements as &$box_element){
    猜你喜欢
    • 2019-04-28
    • 2016-06-05
    • 1970-01-01
    • 1970-01-01
    • 2014-01-06
    • 2018-07-31
    • 2017-10-05
    • 2019-12-01
    • 2021-04-16
    相关资源
    最近更新 更多