dtxb75622 2019-05-07 13:05
浏览 16
已采纳

使用另一个阵列创建新的唯一数组

I have a simple product system in my project. I need to get my product option names and product options look like this. "Color" => "Red,Blue,Yellow" "Size" => "S,M,L,XL"

My records in database look like this

Option names  Options    
Color,Size    Red-S        
Color,Size    Blue-S        
Color,Size    Yellow-S       
Color,Size    Red-M        
Color,Size    Blue-M        
Color,Size    Yellow-M        
Color,Size    Red-L

This is my code:

   foreach ($data as $product) {
       $variations = Products::where('group', $product['sku'])->get();

   }
       $count = count($variations);
       if($count > 0){
           $array2 = [];
           $ss = [];

           foreach($variations as $variants){
               $oname = explode(',', $variants['o_name']);
               $option = explode('-', $variants['option']);
               $array = array_combine($oname, $option);
               $array2[] = compact('array');
               $x = count($oname);
               $xz = $x - 1;
               for($i = 0; $i <= $xz; $i++){

                   $xs = $option[$i];

               }
               $ss[] = $xs;

           }

           dd($ss);

Output :

      array:12 [▼
      0 => "S"
      1 => "S"
      2 => "S"
      3 => "M"
      4 => "M"
      5 => "M"
      6 => "L"
      7 => "L"
      8 => "L"
      9 => "XL"
      10 => "XL"
      11 => "XL"
       ]

How can i create array like this

"Color" => "Red,Blue,Yellow"
  • 写回答

2条回答 默认 最新

  • drzyeetvt41077335 2019-05-07 15:17
    关注
    $variations = Products::where('group', $product['sku'])->get();
    
    //This is me emulating your results
    $variations = [];
    $variations[]= ['o_name'=>'Color,Size', 'option' =>'Red-S'];
    $variations[]= ['o_name'=>'Color,Size', 'option' =>'Blue-S'];
    $variations[]= ['o_name'=>'Color,Size', 'option' =>'Yellow-S'];
    $variations[]= ['o_name'=>'Color,Size', 'option' =>'Red-M'];
    $variations[]= ['o_name'=>'Color,Size', 'option' =>'Blue-M'];
    $variations[]= ['o_name'=>'Color,Size', 'option' =>'Yellow-M'];
    $variations[]= ['o_name'=>'Color,Size', 'option' =>'Red-L'];
    
    //get all the possible options here
    $all_options = [];
    foreach ( $variations as $variants) {
        $oname = explode(',', $variants['o_name']);
        $option = explode('-', $variants['option']);
        $all_options[] = array_combine($oname, $option);
    }
    
    //aggregating attributes and values
    $unique = [];
    $keys = array_keys( $all_options[0]);
    foreach ($keys as $key) {
        $unique[$key] = array_map(function($option) use ($key){
            return $option[$key];
        },$all_options);
    }
    
    //selecting unique values
    foreach ($unique as $key => $value) {
        $unique[$key] = array_unique($unique[$key]);
        $unique[$key] = implode(',',$unique[$key]);
    }
    
    var_dump($unique);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘