php foreach 迭代错误

Error in php foreach iteration

我有这个 laravel collection 叫做 $articles_rows

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"
        ]


 ]
    ]

另一方面,我有另一个名为 $cats_and_arts 的 collection,这个 $cat_and_arts collection 有关于 $articles_rows 'element' 字段的额外信息 collection.

这是我的$cats_and_arts collection:

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 ▶}
  }

]

我想用 $articles_rows 元素字段替换 $cats_and_arts CATEGORIESARTICLES

我想要这个最终结果:

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 ▶}
        }
      ]
    ]

这是我的代码,但是,return 我出错了

Undefined index:id in the line '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;
                    }
                }
              }
           }
        }

当您重新分配 $extra_element 的值时,您就是 "removing" 您在 if.

中使用的 属性 id

有2种解决方案:

  1. 将 id 保存在您的对象中。

$article_element = array_merge((array)$extra_element, $article_element);

  1. 修改您的条件以确保 属性 存在于您的数组中。

if (array_key_exists('id', $article_element) && $article_element['id'] == $extra_element->category->id)

您的问题来自于在以下位置使用数组引用迭代器:

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

因为您稍后覆盖了 $article_element 处的整个数组,foreach 迭代无法继续。这可能可以通过同时使用键和值进行迭代并不通过其迭代器变量覆盖数组,而是直接通过其在父数组中的键来覆盖数组来纠正。

// Iterate the loop with a variable for the key
// instead of using a reference
foreach($article['elements'] as $key => $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
        {   
             // Overwrite it by key in the parent array
             $article['elements'][$key] = (array)$extra_element;
        }   
    }   

    if($extra_element->type == 'ARTICLE')
    {   
        if($article_element['id'] == $extra_element->article->id)
        {   
             // And do the same here...
             $article['elements'][$key] = (array)$extra_element;
        }   
    }   
  }
}

注意:我没有在此处包含最外层的循环。据我所知,它不需要修改,尽管数组引用 &$article 可能是不必要的。您也许可以使用

foreach ($article_rows as $article) {...}

因为我在任何地方都看不到您修改了那个外部迭代器。