当前位置:网站首页>一个PHP算法,php数组一个二维数组拆分成多个子数组
一个PHP算法,php数组一个二维数组拆分成多个子数组
2022-08-08 22:18:00 【爱摄影的程序员。】
2020年10月10日17:42:23
真是场景:
条件:
1、名字为张三和李四的num数量不能超过6,超过6就要拆分数组,(数组不一定存在张三李四)
2、数组所有人的num加起来不能超过30,超过30也要拆分成小数组。(小数组个数不限,满足以上2个条件即可)
3、还有一个条件哦,就是张三和李四如果在同一个子数组里面,他们的数量加起来 和也不能大于6。。
代码:
function _csz($arr)
{
$coder = ['6971632090049','6922994301397']; // 大件货物的条码
$date = func_get_args();
$num = (int)$date[2];
if (in_array($date[1], $coder)) {
$jishu = 6; // 大件货物爆满个数
$yushu = $num;
$beishu = 0;
if ($num > $jishu) {
$yushu = $num % $jishu;
$beishu = floor($num / $jishu);
}
} else {
$jishu = 30; // 小件货物爆满个数
$yushu = $num;
$beishu = 0;
if ($num > $jishu) {
$yushu = $num % $jishu;
$beishu = floor($num / $jishu);
}
}
if ($beishu > 0) {
for ($i = 0; $i < $beishu; $i++) {
$ret[$i] = ['name' => $date[0], 'bar_code' => $date[1], 'quantity' => $jishu, 'scan_mode' => $date[3]];
}
}
if ($yushu > 0) {
$ret['yushu'] = ['name' => $date[0], 'bar_code' => $date[1], 'quantity' => $yushu, 'scan_mode' => $date[3]];
}
return $ret;
}
function _hsz($arr, $base_num, &$return,$index)
{
$count = count($arr);
for ($i=0;$i<$count;$i++){
if(!empty($return)){
$num = 0;
foreach ($return[$index] as $key=>$val){
$num += $val['quantity'];
}
if(($num + $arr[$i]['quantity']) > $base_num){
++$index;
$return[$index][] = $arr[$i];
unset($arr[$i]);
if( count($arr) > 0){
$arr = array_values($arr);
return _hsz($arr, $base_num, $return,$index);
}
return $return;
}else{
$return[$index][] = $arr[$i];
unset($arr[$i]);
$arr = array_values($arr);
if(sizeof($arr) > 0){
return _hsz($arr, $base_num, $return,$index);
}
}
}else{
$return[$index][] = $arr[$i];
unset($arr[$i]);
//$arr = array_values($arr);
}
}
return $return;
}
/**
* 自动拣货器,拆分包裹-算法
* 2020-9-22 18:04:59
* @param $item
* @return array
*/
function getGoodsArray($item)
{
$site6 = $site30 = $info = [] ;
foreach ($item as $k => $v){
$arr = call_user_func_array('_csz', $v);
if(isset($arr['yushu'])){
if(in_array($arr['yushu']['bar_code'], ['6971632090049','6922994301397'])){
$site6[] = $arr['yushu'];
}else{
$site30[] = $arr['yushu'];
}
}
unset($arr['yushu']);
if(!empty($arr)){
$info[] = $arr;
}
}
$return6 = $return30 =[];
$index1 = $index2 = 0;
if(!empty($site6)){
_hsz($site6, 6, $return6, $index1);
}
if(!empty($site30)){
_hsz($site30, 30, $return30, $index2);
}
$info_son = [];
if(is_array($info)){
foreach ($info as $in){
$info_son = array_merge($info_son,$in);
}
}
$rrt = array_merge($info_son,$return6,$return30);
return $rrt;
//var_dump($rrt);
//var_dump($rrt);
//var_dump($return6,$return30);
}
模拟数组:
$goods = [
20200702024 => [
"name" => "LP-胶囊雨伞",
"bar_code" => "20200702024",
"quantity" => "1",
"scan_mode" => "5",
],
6970328080043 => [
"name" => "LP-果冻压缩面膜",
"bar_code" => "6970328080043",
"quantity" => "1",
"scan_mode" => "5",
],
6922994301816 => [
"name" => "LP-兰希黎纯棉洁面巾",
"bar_code" => "6922994301816",
"quantity" => "1",
"scan_mode" => "3",
],
6922994300765 => [
"name" => "兰希黎海洋鲜藻微精华水漾面膜",
"bar_code" => "6922994300765",
"quantity" => "5",
"scan_mode" => "4",
],
6922994300192 => [
"name" => "兰希黎洋甘菊舒缓纯露",
"bar_code" => "6922994300192",
"quantity" => "11",
"scan_mode" => "1",
],
];
$rt = getGoodsArray($goods);
var_dump($rt);
exit;
还有个问题:
这个sql可以搜索到东西,frame_code是varchar(64)
select * from order_frame_log where frame_code = 18000013
这个sql搜不到,frame_code是varchar(64),为什么呢
select * from order_frame_log where frame_code = '18000013'
边栏推荐
猜你喜欢
Unity 创建重复使用的子节点,避免生成多个子节点
What is the cURL?
The principle of neural network deep learning - 2
彻底理解 volatile 关键字及应用场景,面试必问,小白都能看懂!
股市预测,销量预测,病毒传播...一个时间序列建模套路搞定全部!
“文化数字化战略新型基础设施暨文化艺术链生态建设发布会”成功召开
《scala 编程(第3版)》学习笔记
C drive space management
Zero Digital Reports Digital Financial Innovation to the Secretary of Hainan Provincial Party Committee
Matlab的下载
随机推荐
Cesium快速上手2-Model图元使用讲解
中断系统结构及中断控制详解
我曾七次鄙视自己的灵魂——纪伯伦
论文阅读 (66):Explainable Deep Feature Embedding Using Multiple Instance Learning for Pathological Image
Unity BMFont自定义字体
基于阿里云的基础架构设施保障(三)IAAS之网络运用
Liquor Daily Question ---- Find the nth Fibonacci number
基于阿里云的基础架构设施保障(四)IAAS进阶实践运用
网上开户佣金万一靠谱吗,网上客户经理开户安全吗
国产GPU大厂景嘉微半年净利润1.25亿元 旗下产品大卖
Move your office environment anywhere with a solid state USB drive
ZERO Technology "Chain on the South"——deeply cultivated in the field of digital finance
What is the cURL?
2.1.5 折叠剪切展开问题
Chrome Proxy Manager Plugin
Unity添加程序集引用
基于.NET6、FreeSql、若依UI、LayUI、Bootstrap构建插件式的CMS
软件设计原则
九大内置对象四大域
测试/开发程序员,如何跳出技术瓶颈?一年两年......