已知数组a和b。
//数组a:array ( 0 => array ( 'cust_no' => '310F6 1VA5A', 'lotno' => '2X15', 'part_count' => '32', ), 1 => array ( 'cust_no' => '310F6 1VA5A', 'lotno' => '2Z25', 'part_count' => '32', ),)//数组b:array ( 0 => array ( 'cust_no' => '310F6 1VA5A', 'total' => '48', ),)
数组a cust_no为310F6 1VA5A时,对应的总量是32+32=64,数组b cust_no为310F6 1VA5A时对应的总量是48。
想求得数组b总量为48时,对应数组a中的哪些内容?数组a剩余的结果是多少?
以这个例子来说,
得到数组a的结果:
//数组a:array ( 0 => array ( 'cust_no' => '310F6 1VA5A', 'lotno' => '2X15', 'part_count' => '32', ), 1 => array ( 'cust_no' => '310F6 1VA5A', 'lotno' => '2Z25', 'part_count' => '16', ),)
剩余数组a的结果:
//数组a:array ( 0 => array ( 'cust_no' => '310F6 1VA5A', 'lotno' => '2X15', 'part_count' => '0', ), 1 => array ( 'cust_no' => '310F6 1VA5A', 'lotno' => '2Z25', 'part_count' => '16', ),)
如何能得到这样的结果?
回复讨论(解决方案)
你之前不是问过这个问题了么?
如果你要对类似数组做很多纵向(column)计算的话,我建议你不如做个行列转换,思路就开阔多了
大不了计算完成后再一次行列转换换回来就是了
你之前不是问过这个问题了么?
如果你要对类似数组做很多纵向(column)计算的话,我建议你不如做个行列转换,思路就开阔多了
大不了计算完成后再一次行列转换换回来就是了
行列转换没用过,能简单举个例子么?
你之前不是问过这个问题了么?
如果你要对类似数组做很多纵向(column)计算的话,我建议你不如做个行列转换,思路就开阔多了
大不了计算完成后再一次行列转换换回来就是了
如果是数据库的行列转换,那是不是涉及到了交叉表?这个交叉表很麻烦啊。
不是数据库,就是php的数组,把一维和二维的key对调
$a[行][列] 转换成 $a[列][行] 而已
这样就可以把 $a[某列] 作为一个一维数组,做加减,交并差,累计都很方便
我记得以前发过的
//数组a:$a = array ( 0 => array ( 'cust_no' => '310F6 1VA5A', 'lotno' => '2X15', 'part_count' => '32', ), 1 => array ( 'cust_no' => '310F6 1VA5A', 'lotno' => '2Z25', 'part_count' => '32', ),); //数组b: $b = array ( 0 => array ( 'cust_no' => '310F6 1VA5A', 'total' => '48', ),);foreach($b as $source) { $num = $source['total']; foreach($a as $i=>$dest) { if($num == 0) break; if($dest['cust_no'] != $source['cust_no']) continue; if($num >= $dest['part_count']) { $num -= $dest['part_count']; $res[] = $dest; $a[$i]['part_count'] = 0; }else { $dest['part_count'] = $num; $res[] = $dest; $a[$i]['part_count'] -= $num; $num = 0; } }}var_export($res);var_export($a);
array ( 0 => array ( 'cust_no' => '310F6 1VA5A', 'lotno' => '2X15', 'part_count' => '32', ), 1 => array ( 'cust_no' => '310F6 1VA5A', 'lotno' => '2Z25', 'part_count' => 16, ),)array ( 0 => array ( 'cust_no' => '310F6 1VA5A', 'lotno' => '2X15', 'part_count' => 0, ), 1 => array ( 'cust_no' => '310F6 1VA5A', 'lotno' => '2Z25', 'part_count' => 16, ),)
不是数据库,就是php的数组,把一维和二维的key对调
$a[行][列] 转换成 $a[列][行] 而已
这样就可以把 $a[某列] 作为一个一维数组,做加减,交并差,累计都很方便
我记得以前发过的
好的,谢谢!我试试。

tomakephpapplicationsfaster,关注台词:1)useopcodeCachingLikeLikeLikeLikeLikePachetoStorePreciledScompiledScriptbyTecode.2)MinimimiedAtabaseSqueriSegrieSqueriSegeriSybysequeryCachingandeffeftExting.3)Leveragephp7 leveragephp7 leveragephp7 leveragephpphp7功能forbettercodeefficy.4)

到ImprovephPapplicationspeed,关注台词:1)启用opcodeCachingwithapCutoredUcescriptexecutiontime.2)实现databasequerycachingusingpdotominiminimizedatabasehits.3)usehttp/2tomultiplexrequlexrequestsandredececonnection.4 limitsclection.4.4

依赖注入(DI)通过显式传递依赖关系,显着提升了PHP代码的可测试性。 1)DI解耦类与具体实现,使测试和维护更灵活。 2)三种类型中,构造函数注入明确表达依赖,保持状态一致。 3)使用DI容器管理复杂依赖,提升代码质量和开发效率。

databasequeryOptimizationinphpinvolVolVOLVESEVERSEVERSTRATEMIESOENHANCEPERANCE.1)SELECTONLYNLYNESSERSAYCOLUMNSTORMONTOUMTOUNSOUDSATATATATATATATATATATRANSFER.3)

phpisusedforsenderemailsduetoitsbuilt-inmail()函数andsupportiveLibrariesLikePhpMailerandSwiftMailer.1)usethemail()functionforbasicemails,butithasimails.2)butithasimimitations.2)

PHP性能瓶颈可以通过以下步骤解决:1)使用Xdebug或Blackfire进行性能分析,找出问题所在;2)优化数据库查询并使用缓存,如APCu;3)使用array_filter等高效函数优化数组操作;4)配置OPcache进行字节码缓存;5)优化前端,如减少HTTP请求和优化图片;6)持续监控和优化性能。通过这些方法,可以显着提升PHP应用的性能。

依赖性注射(DI)InphpisadesignPatternthatManages和ReducesClassDeptions,增强量产生性,可验证性和Maintainability.itallowspasspassingDepentenciesLikEdenceSeconnectionSeconnectionStoclasseconnectionStoclasseSasasasasareTers,interitationApertatingAeseritatingEaseTestingEasingEaseTeStingEasingAndScalability。

cachingimprovesphpermenceByStorcyResultSofComputationsorqucrouctationsorquctationsorquickretrieval,reducingServerLoadAndenHancingResponsetimes.feftectivestrategiesinclude:1)opcodecaching,whereStoresCompiledSinmememorytssinmemorytoskipcompliation; 2)datacaching datacachingsingMemccachingmcachingmcachings


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

WebStorm Mac版
好用的JavaScript开发工具

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具