【问题标题】:Merge array inside different collections inside same array合并同一数组内不同集合内的数组
【发布时间】:2020-11-26 13:22:27
【问题描述】:

我得到了一个包含集合的数组,在这些集合中又有数组。我想要做的是将集合合并为只有一个集合与多个集合的所有数组。

Collection {#1592 ▼
  #items: array:4 [▼
    0 => Collection {#1595 ▼
      #items: array:2 [▶]
    }
    1 => Collection {#1589 ▶}
    2 => Collection {#1585 ▼
      #items: array:2 [▶]
    }
    3 => Collection {#1579 ▼
      #items: array:2 [▶]
    }
  ]
}

【问题讨论】:

    标签: arrays laravel collections


    【解决方案1】:

    您可以使用flatten() 集合方法:

    即)

    $a = collect(['a', 'b', 'c']);
    $d = collect(['d', 'e', 'f']);
    $g = collect(['g', 'h', 'i']);
    
    $c = collect([$a, $d, $g]);
    
    $c->flatten();
    

    将输出:

    Illuminate\Support\Collection {#3124
        all: [
            "a",
            "b",
            "c",
            "d",
            "e",
            "f",
            "g",
            "h",
            "i",
        ],
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多