php 以数组为参数 获取对应的数组

function cart_by_favourable_project_list($merchant_goods)
{   
 $id_list = array();
 $list_array = array();    //根据购物车中的商品数据获取所有商品的顶级cat_id与顶级catname

 foreach ($merchant_goods as $key => $row) {
  $user_cart_goods = isset($row['goods_list']) && !empty($row['goods_list']) ? $row['goods_list'] : array();
  //1.取出第三级分类的id  foreach($user_cart_goods as $key1 => $row1){
      $user_cart_goods_cat_id_3[] = $row1['cat_id'];
  }
  
  //2.取出第二级分类的id  $user_cart_goods_cat_id_3_str = join(',', $user_cart_goods_cat_id_3);
  $user_cart_goods_cat_id_2_sql = "select cat_id,parent_id from dsc_category where cat_id in ($user_cart_goods_cat_id_3_str)";
  $res_user_cart_goods_cat_id_2 = $GLOBALS['db']->query($user_cart_goods_cat_id_2_sql);

        while($row_user_cart_goods_cat_id_2 = $GLOBALS['db']->fetchRow($res_user_cart_goods_cat_id_2)){
            //商品分类cate_id的parent_id
            $user_cart_goods_cat_id_2[$row_user_cart_goods_cat_id_2['cat_id']]=$row_user_cart_goods_cat_id_2['parent_id'];
        }
        
        $user_cart_goods_cat_id_2 = array_map(function($id) use ($user_cart_goods_cat_id_2){
            return $user_cart_goods_cat_id_2[$id];
        },$user_cart_goods_cat_id_3);
       
        //3.取出顶级分类的id        $user_cart_goods_cat_id_2_str = join(',',$user_cart_goods_cat_id_2);
        $user_cart_goods_cat_id_1_sql = "select cat_id,parent_id from dsc_category where cat_id in ($user_cart_goods_cat_id_2_str)";
        $res_user_cart_goods_cat_id_1 = $GLOBALS['db']->query($user_cart_goods_cat_id_1_sql);
        
        while($row_user_cart_goods_cat_id_1 = $GLOBALS['db']->fetchRow($res_user_cart_goods_cat_id_1)){
            //商品分类cate_id的parent_id
            $user_cart_goods_cat_id_1[$row_user_cart_goods_cat_id_1['cat_id']]=$row_user_cart_goods_cat_id_1['parent_id'];
        }
        
        $user_cart_goods_cat_id_1 = array_map(function($id) use ($user_cart_goods_cat_id_1){
            return $user_cart_goods_cat_id_1[$id];
        },$user_cart_goods_cat_id_2);
        
        //4.取出顶级分类的cat_name        $final_cat_name_sql="select cat_id,cat_name from dsc_category where cat_id in (" . join(",",$user_cart_goods_cat_id_1) . ")";

        $res_final_cat_name_array = $GLOBALS['db']->query($final_cat_name_sql);
        
        while($row_final_cat_name_array = $GLOBALS['db']->fetchRow($res_final_cat_name_array)){
            //商品分类cate_id的parent_id
            $row_final_cat_name_array_1[$row_final_cat_name_array['cat_id']] = $row_final_cat_name_array['cat_name'];
        }
        
       
        $row_final_cat_name_array_1 = array_map(function($id) use ($row_final_cat_name_array_1) {
            return $row_final_cat_name_array_1[$id];
        }, $user_cart_goods_cat_id_1);
        
        //5.$row_final_cat_name_array_1数组就是顶级cat_name

  $favourable_list = favourable_list($_SESSION['user_rank'], $row['ru_id']);
  $sort_favourable = sort_favourable($favourable_list);

        $index = 0 ;
        while(count($row_final_cat_name_array_1)-$index){
            $user_cart_goods[$index]['cat_name']=$row_final_cat_name_array_1[$index];
            $index++;
        }
        
        
        $index1 = 0 ;
        while(count($user_cart_goods_cat_id_1)-$index1){
            $user_cart_goods[$index1]['top_id']=$user_cart_goods_cat_id_1[$index1];
            $index1++;
        }
        

  if ($user_cart_goods) {
      
   foreach ($user_cart_goods as $key1 => $row1) {
       
    $row1['original_price'] = $row1['goods_price'] * $row1['goods_number'];
    if (isset($sort_favourable['by_all']) && $row1['extension_code'] != 'package_buy' && substr($row1['extension_code'], 0, 7) != 'seckill') {
     foreach ($sort_favourable['by_all'] as $key2 => $row2) {
      $mer_ids = true;

      if ($GLOBALS['_CFG']['region_store_enabled']) {
       $mer_ids = get_favourable_merchants($row2['userFav_type'], $row2['userFav_type_ext'], $row2['rs_id'], 1, $row1['ru_id']);
      }

      if ($row2['userFav_type'] == 1 || $mer_ids) {
       if ($row1['is_gift'] == 0) {
        if (isset($row1) && $row1) {
         $merchant_goods[$key]['new_list'][$row2['act_id']]['act_id'] = $row2['act_id'];
         $merchant_goods[$key]['new_list'][$row2['act_id']]['act_name'] = $row2['act_name'];
         $merchant_goods[$key]['new_list'][$row2['act_id']]['act_type'] = $row2['act_type'];

         switch ($row2['act_type']) {
         case 0:
          $merchant_goods[$key]['new_list'][$row2['act_id']]['act_type_txt'] = $GLOBALS['_LANG']['With_a_gift'];
          $merchant_goods[$key]['new_list'][$row2['act_id']]['act_type_ext_format'] = intval($row2['act_type_ext']);
          break;

         case 1:
          $merchant_goods[$key]['new_list'][$row2['act_id']]['act_type_txt'] = $GLOBALS['_LANG']['Full_reduction'];
          $merchant_goods[$key]['new_list'][$row2['act_id']]['act_type_ext_format'] = number_format($row2['act_type_ext'], 2);
          break;

         case 2:
          $merchant_goods[$key]['new_list'][$row2['act_id']]['act_type_txt'] = $GLOBALS['_LANG']['discount'];
          $merchant_goods[$key]['new_list'][$row2['act_id']]['act_type_ext_format'] = floatval($row2['act_type_ext'] / 10);
          break;

         default:
          break;
         }

         $merchant_goods[$key]['new_list'][$row2['act_id']]['min_amount'] = $row2['min_amount'];
         $merchant_goods[$key]['new_list'][$row2['act_id']]['act_type_ext'] = intval($row2['act_type_ext']);
         @$merchant_goods[$key]['new_list'][$row2['act_id']]['cart_fav_amount'] += $row1['subtotal'];
         $merchant_goods[$key]['new_list'][$row2['act_id']]['available'] = favourable_available($row2, array(), $row1['ru_id']);
         $cart_favourable = cart_favourable($row1['ru_id']);
         $merchant_goods[$key]['new_list'][$row2['act_id']]['cart_favourable_gift_num'] = empty($cart_favourable[$row2['act_id']]) ? 0 : intval($cart_favourable[$row2['act_id']]);
         $merchant_goods[$key]['new_list'][$row2['act_id']]['favourable_used'] = favourable_used($row2, $cart_favourable);
         $merchant_goods[$key]['new_list'][$row2['act_id']]['left_gift_num'] = intval($row2['act_type_ext']) - (empty($cart_favourable[$row2['act_id']]) ? 0 : intval($cart_favourable[$row2['act_id']]));

         if ($row2['gift']) {
          $merchant_goods[$key]['new_list'][$row2['act_id']]['act_gift_list'] = $row2['gift'];
         }

         $merchant_goods[$key]['new_list'][$row2['act_id']]['act_goods_list'][$row1['rec_id']] = $row1;
         unset($row1);

         if (defined('THEME_EXTENSION')) {
          $merchant_goods[$key]['new_list'][$row2['act_id']]['act_goods_list_num'] = count($merchant_goods[$key]['new_list'][$row2['act_id']]['act_goods_list']);
         }
        }
       }
       else {
        $merchant_goods[$key]['new_list'][$row2['act_id']]['act_cart_gift'][$row1['rec_id']] = $row1;
       }
      }
      else if ($GLOBALS['_CFG']['region_store_enabled']) {
       $merchant_goods[$key]['new_list'][0]['act_goods_list'][$row1['rec_id']] = $row1;

       if (defined('THEME_EXTENSION')) {
        $merchant_goods[$key]['new_list'][0]['act_goods_list_num'] = count($merchant_goods[$key]['new_list'][0]['act_goods_list']);
       }
      }

      break;
     }

     continue;
    }

    if (isset($sort_favourable['by_category']) && $row1['extension_code'] != 'package_buy' && substr($row1['extension_code'], 0, 7) != 'seckill') {
     $get_act_range_ext = get_act_range_ext($_SESSION['user_rank'], $row['ru_id'], 1);
     $str_cat = '';

     foreach ($get_act_range_ext as $id) {
      $cat_keys = get_array_keys_cat(intval($id));

      if ($cat_keys) {
       $str_cat .= implode(',', $cat_keys);
      }
     }

     if ($str_cat) {
      $list_array = explode(',', $str_cat);
     }

     $list_array = !empty($list_array) ? array_merge($get_act_range_ext, $list_array) : $get_act_range_ext;
     $id_list = arr_foreach($list_array);
     $id_list = array_unique($id_list);
     $cat_id = $row1['cat_id'];
     $favourable_id_list = get_favourable_id($sort_favourable['by_category']);
     if (in_array($cat_id, $id_list) && $row1['is_gift'] == 0 || in_array($row1['is_gift'], $favourable_id_list)) {
      foreach ($sort_favourable['by_category'] as $key2 => $row2) {
       if (isset($row1) && $row1) {
        $fav_act_range_ext = !empty($row2['act_range_ext']) ? explode(',', $row2['act_range_ext']) : array();

        foreach ($fav_act_range_ext as $id) {
         $cat_keys = get_array_keys_cat(intval($id));
         $fav_act_range_ext = array_merge($fav_act_range_ext, $cat_keys);
        }

        if ($row1['is_gift'] == 0 && in_array($cat_id, $fav_act_range_ext)) {
         $merchant_goods[$key]['new_list'][$row2['act_id']]['act_id'] = $row2['act_id'];
         $merchant_goods[$key]['new_list'][$row2['act_id']]['act_name'] = $row2['act_name'];
         $merchant_goods[$key]['new_list'][$row2['act_id']]['act_type'] = $row2['act_type'];

         switch ($row2['act_type']) {
         case 0:
          $merchant_goods[$key]['new_list'][$row2['act_id']]['act_type_txt'] = $GLOBALS['_LANG']['With_a_gift'];
          $merchant_goods[$key]['new_list'][$row2['act_id']]['act_type_ext_format'] = intval($row2['act_type_ext']);
          break;

         case 1:
          $merchant_goods[$key]['new_list'][$row2['act_id']]['act_type_txt'] = $GLOBALS['_LANG']['Full_reduction'];
          $merchant_goods[$key]['new_list'][$row2['act_id']]['act_type_ext_format'] = number_format($row2['act_type_ext'], 2);
          break;

         case 2:
          $merchant_goods[$key]['new_list'][$row2['act_id']]['act_type_txt'] = $GLOBALS['_LANG']['discount'];
          $merchant_goods[$key]['new_list'][$row2['act_id']]['act_type_ext_format'] = floatval($row2['act_type_ext'] / 10);
          break;

         default:
          break;
         }

         $merchant_goods[$key]['new_list'][$row2['act_id']]['min_amount'] = $row2['min_amount'];
         $merchant_goods[$key]['new_list'][$row2['act_id']]['act_type_ext'] = intval($row2['act_type_ext']);
         @$merchant_goods[$key]['new_list'][$row2['act_id']]['cart_fav_amount'] += $row1['subtotal'];
         $merchant_goods[$key]['new_list'][$row2['act_id']]['available'] = favourable_available($row2, array(), $row1['ru_id']);
         $cart_favourable = cart_favourable($row1['ru_id']);
         $merchant_goods[$key]['new_list'][$row2['act_id']]['cart_favourable_gift_num'] = empty($cart_favourable[$row2['act_id']]) ? 0 : intval($cart_favourable[$row2['act_id']]);
         $merchant_goods[$key]['new_list'][$row2['act_id']]['favourable_used'] = favourable_used($row2, $cart_favourable);
         $merchant_goods[$key]['new_list'][$row2['act_id']]['left_gift_num'] = intval($row2['act_type_ext']) - (empty($cart_favourable[$row2['act_id']]) ? 0 : intval($cart_favourable[$row2['act_id']]));

         if ($row2['gift']) {
          $merchant_goods[$key]['new_list'][$row2['act_id']]['act_gift_list'] = $row2['gift'];
         }

         $merchant_goods[$key]['new_list'][$row2['act_id']]['act_goods_list'][$row1['rec_id']] = $row1;
                                    
                                    
                                    
         if (defined('THEME_EXTENSION')) {
          $merchant_goods[$key]['new_list'][$row2['act_id']]['act_goods_list_num'] = count($merchant_goods[$key]['new_list'][$row2['act_id']]['act_goods_list']);
         }

         unset($row1);
        }

        if (isset($row1) && $row1 && $row1['is_gift'] == $row2['act_id']) {
         $merchant_goods[$key]['new_list'][$row2['act_id']]['act_cart_gift'][$row1['rec_id']] = $row1;
        }
       }
      }

      continue;
     }
    }

    if (isset($sort_favourable['by_brand']) && $row1['extension_code'] != 'package_buy' && substr($row1['extension_code'], 0, 7) != 'seckill') {
     $get_act_range_ext = get_act_range_ext($_SESSION['user_rank'], $row['ru_id'], 2);
     $brand_id = $row1['brand_id'];
     $favourable_id_list = get_favourable_id($sort_favourable['by_brand']);
     if (in_array(trim($brand_id), $get_act_range_ext) && $row1['is_gift'] == 0 || in_array($row1['is_gift'], $favourable_id_list)) {
      foreach ($sort_favourable['by_brand'] as $key2 => $row2) {
       $act_range_ext_str = ',' . $row2['act_range_ext'] . ',';
       $brand_id_str = ',' . $brand_id . ',';
       if (isset($row1) && $row1) {
        if ($row1['is_gift'] == 0 && strstr($act_range_ext_str, trim($brand_id_str))) {
         $merchant_goods[$key]['new_list'][$row2['act_id']]['act_id'] = $row2['act_id'];
         $merchant_goods[$key]['new_list'][$row2['act_id']]['act_name'] = $row2['act_name'];
         $merchant_goods[$key]['new_list'][$row2['act_id']]['act_type'] = $row2['act_type'];

         switch ($row2['act_type']) {
         case 0:
          $merchant_goods[$key]['new_list'][$row2['act_id']]['act_type_txt'] = $GLOBALS['_LANG']['With_a_gift'];
          $merchant_goods[$key]['new_list'][$row2['act_id']]['act_type_ext_format'] = intval($row2['act_type_ext']);
          break;

         case 1:
          $merchant_goods[$key]['new_list'][$row2['act_id']]['act_type_txt'] = $GLOBALS['_LANG']['Full_reduction'];
          $merchant_goods[$key]['new_list'][$row2['act_id']]['act_type_ext_format'] = number_format($row2['act_type_ext'], 2);
          break;

         case 2:
          $merchant_goods[$key]['new_list'][$row2['act_id']]['act_type_txt'] = $GLOBALS['_LANG']['discount'];
          $merchant_goods[$key]['new_list'][$row2['act_id']]['act_type_ext_format'] = floatval($row2['act_type_ext'] / 10);
          break;

         default:
          break;
         }

         $merchant_goods[$key]['new_list'][$row2['act_id']]['min_amount'] = $row2['min_amount'];
         $merchant_goods[$key]['new_list'][$row2['act_id']]['act_type_ext'] = intval($row2['act_type_ext']);
         @$merchant_goods[$key]['new_list'][$row2['act_id']]['cart_fav_amount'] += $row1['subtotal'];
         $merchant_goods[$key]['new_list'][$row2['act_id']]['available'] = favourable_available($row2);
         $cart_favourable = cart_favourable($row1['ru_id']);
         $merchant_goods[$key]['new_list'][$row2['act_id']]['cart_favourable_gift_num'] = empty($cart_favourable[$row2['act_id']]) ? 0 : intval($cart_favourable[$row2['act_id']]);
         $merchant_goods[$key]['new_list'][$row2['act_id']]['favourable_used'] = favourable_used($row2, $cart_favourable);
         $merchant_goods[$key]['new_list'][$row2['act_id']]['left_gift_num'] = intval($row2['act_type_ext']) - (empty($cart_favourable[$row2['act_id']]) ? 0 : intval($cart_favourable[$row2['act_id']]));

         if ($row2['gift']) {
          $merchant_goods[$key]['new_list'][$row2['act_id']]['act_gift_list'] = $row2['gift'];
         }

         $merchant_goods[$key]['new_list'][$row2['act_id']]['act_goods_list'][$row1['rec_id']] = $row1;

         if (defined('THEME_EXTENSION')) {
          $merchant_goods[$key]['new_list'][$row2['act_id']]['act_goods_list_num'] = count($merchant_goods[$key]['new_list'][$row2['act_id']]['act_goods_list']);
         }

         unset($row1);
        }

        if (isset($row1) && $row1 && $row1['is_gift'] == $row2['act_id']) {
         $merchant_goods[$key]['new_list'][$row2['act_id']]['act_cart_gift'][$row1['rec_id']] = $row1;
        }
       }
      }

      continue;
     }
    }

    if (isset($sort_favourable['by_goods']) && $row1['extension_code'] != 'package_buy' && substr($row1['extension_code'], 0, 7) != 'seckill') {
     $get_act_range_ext = get_act_range_ext($_SESSION['user_rank'], $row['ru_id'], 3);
     $favourable_id_list = get_favourable_id($sort_favourable['by_goods']);
     if (in_array($row1['goods_id'], $get_act_range_ext) || in_array($row1['is_gift'], $favourable_id_list)) {
      foreach ($sort_favourable['by_goods'] as $key2 => $row2) {
       $act_range_ext_str = ',' . $row2['act_range_ext'] . ',';
       $goods_id_str = ',' . $row1['goods_id'] . ',';
       if (isset($row1) && $row1) {
        if (strstr($act_range_ext_str, $goods_id_str) && $row1['is_gift'] == 0) {
         $merchant_goods[$key]['new_list'][$row2['act_id']]['act_id'] = $row2['act_id'];
         $merchant_goods[$key]['new_list'][$row2['act_id']]['act_name'] = $row2['act_name'];
         $merchant_goods[$key]['new_list'][$row2['act_id']]['act_type'] = $row2['act_type'];

         switch ($row2['act_type']) {
         case 0:
          $merchant_goods[$key]['new_list'][$row2['act_id']]['act_type_txt'] = $GLOBALS['_LANG']['With_a_gift'];
          $merchant_goods[$key]['new_list'][$row2['act_id']]['act_type_ext_format'] = intval($row2['act_type_ext']);
          break;

         case 1:
          $merchant_goods[$key]['new_list'][$row2['act_id']]['act_type_txt'] = $GLOBALS['_LANG']['Full_reduction'];
          $merchant_goods[$key]['new_list'][$row2['act_id']]['act_type_ext_format'] = number_format($row2['act_type_ext'], 2);
          break;

         case 2:
          $merchant_goods[$key]['new_list'][$row2['act_id']]['act_type_txt'] = $GLOBALS['_LANG']['discount'];
          $merchant_goods[$key]['new_list'][$row2['act_id']]['act_type_ext_format'] = floatval($row2['act_type_ext'] / 10);
          break;

         default:
          break;
         }

         $merchant_goods[$key]['new_list'][$row2['act_id']]['min_amount'] = $row2['min_amount'];
         $merchant_goods[$key]['new_list'][$row2['act_id']]['act_type_ext'] = intval($row2['act_type_ext']);
         @$merchant_goods[$key]['new_list'][$row2['act_id']]['cart_fav_amount'] += $row1['subtotal'];
         $merchant_goods[$key]['new_list'][$row2['act_id']]['available'] = favourable_available($row2);
         $cart_favourable = cart_favourable($row1['ru_id']);
         $merchant_goods[$key]['new_list'][$row2['act_id']]['cart_favourable_gift_num'] = empty($cart_favourable[$row2['act_id']]) ? 0 : intval($cart_favourable[$row2['act_id']]);
         $merchant_goods[$key]['new_list'][$row2['act_id']]['favourable_used'] = favourable_used($row2, $cart_favourable);
         $merchant_goods[$key]['new_list'][$row2['act_id']]['left_gift_num'] = intval($row2['act_type_ext']) - (empty($cart_favourable[$row2['act_id']]) ? 0 : intval($cart_favourable[$row2['act_id']]));

         if ($row2['gift']) {
          $merchant_goods[$key]['new_list'][$row2['act_id']]['act_gift_list'] = $row2['gift'];
         }

         $merchant_goods[$key]['new_list'][$row2['act_id']]['act_goods_list'][$row1['rec_id']] = $row1;

         if (defined('THEME_EXTENSION')) {
          $merchant_goods[$key]['new_list'][$row2['act_id']]['act_goods_list_num'] = count($merchant_goods[$key]['new_list'][$row2['act_id']]['act_goods_list']);
         }

         break;
         unset($row1);
        }

        if (isset($row1) && $row1 && $row1['is_gift'] == $row2['act_id']) {
         $merchant_goods[$key]['new_list'][$row2['act_id']]['act_cart_gift'][$row1['rec_id']] = $row1;
        }
       }
      }
     }
     else {
      $merchant_goods[$key]['new_list'][0]['act_goods_list'][$row1['rec_id']] = $row1;

      if (defined('THEME_EXTENSION')) {
       $merchant_goods[$key]['new_list'][0]['act_goods_list_num'] = count($merchant_goods[$key]['new_list'][0]['act_goods_list']);
      }
     }
    }
    else {
     $merchant_goods[$key]['new_list'][0]['act_goods_list'][$row1['rec_id']] = $row1;

     if (defined('THEME_EXTENSION')) {
      $merchant_goods[$key]['new_list'][0]['act_goods_list_num'] = count($merchant_goods[$key]['new_list'][0]['act_goods_list']);
     }
    }
   }
  }
 }
    
    
    // echo "<pre>";
    // var_dump($merchant_goods);
    // die;
    
    
    
    $new_goods_list = $merchant_goods[0]['new_list'][0]['act_goods_list'];
    
    rsort($new_goods_list);
    
    $merchant_goods[0]['new_list'][0]['act_goods_list']=$new_goods_list;
    
    
    // echo "<pre>";
    // var_dump($merchant_goods[0]['new_list'][0]['act_goods_list']);
    $new_goods_list2 = [];
    foreach ($merchant_goods[0]['new_list'][0]['act_goods_list'] as $item) {
        $new_goods_list2[$item['top_id']]['name'] = $item['cat_name'];
        $new_goods_list2[$item['top_id']]['goods_list'][] = $item;
    }
    $merchant_goods[0]['new_list'][0]['act_goods_list'] = $new_goods_list2;
 return $merchant_goods;
 
}

有帮助(- 没帮助(-