php foreach迭代出错

时间:2016-12-19 12:17:52

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

我有这个名为$articles_rows的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"
        ]


 ]
    ]

另一方面,我有另一个名为$cats_and_arts的集合,此$cat_and_arts collection有关于&#39;元素的额外信息。 $articles_rows集合中的字段。

这是我的$ cats_and_arts集合:

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

]

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

我想要这个最终结果:

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

这是我的代码,但是,给我一个错误

  

未定义的索引:行中的ID&#39; if($ article_element [&#39; id&#39;] ==   $ extra_element-&GT;分类 - &GT; ID)&#39;

当循环有多次迭代时:

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

2 个答案:

答案 0 :(得分:1)

当您重新分配$extra_element的值时,您将“删除”您在if中使用的属性ID。

有两种解决方案:

  1. 将ID保留在您的对象中。
  2. $article_element = array_merge((array)$extra_element, $article_element);

    1. 修改您的条件以确保您的数组中存在该属性。
    2. if (array_key_exists('id', $article_element) && $article_element['id'] == $extra_element->category->id)

答案 1 :(得分:0)

您的问题来自于使用数组引用迭代器:

$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) {...} 可能是不必要的,但据我所知,它不需要修改。您可以使用

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
  //the parser found some characters in-between an opening and closing tag
  //what are you going to do?
}

因为我没有看到你修改那个外部迭代器的任何地方。