2016-12-19 2 views
0

$ box_items이라는 laravel 컬렉션이 있습니다. 한편 두 개의 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" 
     ] 
     ] 
    ] 

, 나는 또 다른 컬렉션이 $ 요소 수집은 $ box_items 컬렉션의 '요소'필드에 대한 추가 정보를 가지고 $ 요소라고했다.

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 요소 필드에 의해 $ 요소를 범주와 기사를 교체하려면 :

이 내 $ 요소 모음입니다.

I는이 최종 결과를 원한다

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

EDIT : 라인에 에러의 ($ article_element [ 'ID'] == $ extra_element-> 범주 -> 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); 

답변

0

그것은 당신을 위해 일해야합니다.

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

하지만 최종 결과 –

+0

처럼?, 모든 정보를 저장할 수있는 곳은 $에서 사용할 수는 항목이 –

+0

확인 업데이트를보기 위해 참조하는 DD ($ box_items)에 의해 전달되는 box_items이 나에게이를 반환 오류 : "개체가 아닌 개체의 속성을 수정하려고했습니다."필드 요소에 배열이 아닌 개체가 포함되어 있기 때문에 ... –

관련 문제