标签:lis convert 元素 serial inter int 不同的 ++ 遍历数组
public function getDisabledSpec($goods_info,$spec_arr){ if(empty($spec_arr)){ $spec_arr = array_keys($goods_info[‘goods_spec‘]);//用户已选择的规格值 }else{ $spec_arr = array_values($spec_arr);//用户已选择的规格值 function convertToInt($value){ return intval($value); } $spec_arr = array_map("convertToInt",$spec_arr); } if(empty($goods_info[‘goods_spec‘]) || empty($goods_info[‘spec_value‘])) return array(); $goods_list = Model(‘goods‘)->getGoodsList(array(‘goods_commonid‘ => $goods_info[‘goods_commonid‘])); $disable_goods = array(); $all_goods = array(); $result = array(); $all_spec_key = array(); foreach($goods_list as $key => $goods_item){ $goods_spec_key = array_keys(unserialize($goods_item[‘goods_spec‘])); $all_goods[] = array(‘spec_key‘ => $goods_spec_key,‘storage‘ => $goods_item[‘goods_storage‘]); $all_spec_key = array_merge($all_spec_key,$goods_spec_key); if(intval($goods_item[‘goods_storage‘]) <= 0) { $disable_goods[] = $goods_spec_key; } } $all_spec_key = array_unique($all_spec_key); // $all_spec_key = array_diff($all_spec_key,$spec_arr); //若两者相等,则此spu下所有的sku规格值的库存都为空 if(count($disable_goods) == count($goods_list)){ return $all_spec_key; } $intersect_arr = array(); $disable_arr = array(); foreach($disable_goods as $key => $goods_item){ $intersect = array_intersect($spec_arr, $goods_item); if(empty($intersect)) continue; $intersect_arr[] = $intersect; $disable_arr = array_merge($disable_arr,$goods_item); } $disable_arr = array_values(array_unique($disable_arr)); $handle_arr = array(); for($i = 0;$i < count($intersect_arr);$i++){ if(empty($intersect_arr[$i])) continue; if(in_array($intersect_arr[$i],$handle_arr)) continue; array_push($handle_arr,$intersect_arr[$i]); for($j=0;$j < count($disable_arr); $j++){ if(!in_array($disable_arr[$j],$intersect_arr[$i])){ $this->_handleSpec($result,$all_goods,$intersect_arr[$i],$disable_arr[$j]); } } } /** * 最后两步 * * 1. 去重 * 2. 去掉勾选的规格值 */ $result = array_unique($result); $result = array_diff($all_spec_key,$result); $result = array_values($result); unset($handle_arr); unset($disable_arr); unset($intersect_arr); unset($all_spec_key); unset($all_goods); unset($goods_list); unset($disable_goods); return $result; } private function _handleSpec(&$result,$all_goods,$intersect,$disable_key){ $flag = 0; $intersect = array_values($intersect); array_push($intersect,$disable_key); foreach($all_goods as $key => $goods_item){ if(count(array_intersect($intersect,$goods_item[‘spec_key‘])) == count($intersect)){ $flag = 1; if($goods_item[‘storage‘] != 0){ $flag = 2;break; } } } //处理 if($flag == 1){ array_push($result,$disable_key); } }
标签:lis convert 元素 serial inter int 不同的 ++ 遍历数组
原文地址:https://www.cnblogs.com/shilin000/p/9395136.html