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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?