散分?
从数据库读取数据,多数为一个行列整齐的二维数组,姑且叫它做矩阵数组吧
当读取后用php计算涉及很多纵向计算(例如对某字段求和),我看很多人都习惯 $array[记录][字段],其实读取时就应该想好怎样分配数组一维和二维的key
有时获取的矩阵数组格式固定了,例如来自某个API,我看到不少人还按着记录去求二维的值,更有甚者还用递归,看着真累,把行列交换一下再计算多方便啊
squareArray.php
<?phpclass squareArray{ public function swapRowCol($inArr) { $mit = new MultipleIterator(MultipleIterator::MIT_KEYS_ASSOC); foreach($inArr as $key => $value) { $temArr = new ArrayIterator($value); $mit->attachIterator($temArr, $key); } $arr = array(); foreach($mit as $item) array_push($arr,$item); if (isset($inArr[0])) $arr = array_combine(array_keys($inArr[0]),$arr); return $arr; } public function swapRowColWithKey($inArr) { foreach($inArr as $k1=>$v1) foreach($v1 as $k2=>$v2) $arr[$k2][$k1] = $v2; return $arr; } public function intersect1st($arr1, $arr2, $key) { $arr[$key] = array_intersect($arr1[$key], $arr2[$key]);//match source's $key with target foreach($arr1 as $k=>$v) { if ($k == $key) continue; $arr[$k] = array_intersect_key($v, $arr[$key]); } return $arr; } public function intersect2nd($arr1, $arr2, $key) { foreach($arr2 as $v) $tmpArr[] = $v[$key]; foreach($arr1 as $k=>$v) if(in_array($v[$key], $tmpArr)) $arr[$k] = $v; return $arr; }}?>
squareArray.sample.php
这是使用方法,写得简单了点,只是为了怕自己太久没用忘了,提醒一下写法而已
<?phpinclude('squareArray.php');//$array = array('id' => array('001', '002', '003'),'name' => array('张三', '李四', '王五'),'age' => array(22, 23, 11)); $array = array(array('id' => '001', 'name' => '张三', 'age' => 22),array('id' => '002', 'name' => '李四', 'age' => 23),array('id' => '003', 'name' => '王五', 'age' => 11)); $a = array( 0 => array('action_id' => 3), 1 => array('action_id' => 2), 2 => array('action_id' => 1), 3 => array('action_id' => 7), 4 => array('action_id' => 11),);$b = array( 0 => array('action_id' => 3, 'type' => 0, 'order_num' => 67), 1 => array('action_id' => 2, 'type' => 0, 'order_num' => 66), 2 => array('action_id' => 1, 'type' => 0, 'order_num' => 65), 3 => array('action_id' => 7, 'type' => 0, 'order_num' => 64), 8 => array('action_id' => 14, 'type' => 0, 'order_num' => 40), 13 => array('action_id' => 11, 'type' => 0, 'order_num' => 30),);//交换矩阵数组一维和二维键值例子$obj=new squareArray();$arr=$obj->swapRowCol($array);var_export($arr);//交换矩阵数组一维和二维键值例子(保留数值键名)$arr=$obj->swapRowColWithKey($b);var_export($arr);//根据指定key求二维数组矩阵数组交集(一维key)$key='action_id';$aa=$obj->swapRowCol($a);$bb=$obj->swapRowCol($b);$arr = $obj->intersect1st($bb, $aa, $key);//$b和$a自己定义吧,我懒得输入了$arr=$obj->swapRowCol($arr);var_export($arr);//根据指定key求二维数组矩阵数组交集(二维key)$key='action_id';$arr = $obj->intersect2nd($b, $a, $key);//$b和$a自己定义吧,我懒得输入了var_export($arr);exit;?>
如果发现bug就自己处理吧……
回复讨论(解决方案)
mark!
学习一下 感谢分享
巢状数组迭代该如何写?
巢状数组迭代该如何写?
那就不是我这个考虑的了,你也来贡献一个
巢状数组是啥样的?
不考虑key的swapRowCol方法为啥比考虑key的swapRowColWithKey还复杂呢?
我经常这么用
public function swapRowCol($inArr) { return call_user_func_array('array_merge_recursive', $inArr); }
巢状数组是啥样的?
不考虑key的swapRowCol方法为啥比考虑key的swapRowColWithKey还复杂呢?
我经常这么用
public function swapRowCol($inArr) { return call_user_func_array('array_merge_recursive', $inArr); }
呵呵,如果输出正确当然你这个好
我当时是顺便练习SPL写的,之后就基本没用过swapRowCol(),一直都是用swapRowColWithKey(),所以就没再考虑优化了
巢状数组是啥样的?
巢状我没理解错的话就是阵列中还嵌套阵列(cell arrays? nested arrays?),维度细致化来说就是超出二维了
巢状数组是啥样的?
巢状我没理解错的话就是阵列中还嵌套阵列(cell arrays? nested arrays?),维度细致化来说就是超出二维了
那如果不考虑key的话用#5方法不是正好
接触php一年了,第一次看到这个对象
$mit = new MultipleIterator(MultipleIterator::MIT_KEYS_ASSOC);
咋整啊......
学习了,记号一下
收藏+学习。
谢谢lz分享
好铁。先搜藏了
很拥挤啊。换行,大括号。
很拥挤啊。换行,大括号。
显示器1920像素,所以,换行更挤
过来学习一下
接触php一年了,第一次看到这个对象
$mit = new MultipleIterator(MultipleIterator::MIT_KEYS_ASSOC);
咋整啊......
这句啥意思
好东东,值得收藏之
接触php一年了,第一次看到这个对象
$mit = new MultipleIterator(MultipleIterator::MIT_KEYS_ASSOC);
咋整啊......
这句啥意思
自己查查手册 SPL一章
php5.5 增加了一个array_column函数,这个可以用用
php5.5 增加了一个array_column函数,这个可以用用
我也是昨天中午答人家一个问题查手册才看到,感觉从没用过,细看才知道新增加的
这咚咚不错,减少不少filter和遍历
谁给我的回复删了?
array_column ? 返回数组中指定的一列
自己写一个也很容易
$b = array( 0 => array('action_id' => 3, 'type' => 0, 'order_num' => 67), 1 => array('action_id' => 2, 'type' => 0, 'order_num' => 66), 2 => array('action_id' => 1, 'type' => 0, 'order_num' => 65), 3 => array('action_id' => 7, 'type' => 0, 'order_num' => 64), 8 => array('action_id' => 14, 'type' => 0, 'order_num' => 40), 13 => array('action_id' => 11, 'type' => 0, 'order_num' => 30),);print_r(array_column($b, 'action_id'));function array_column($ar, $key) { return array_map(function($item) use ($key) { return @$item[$key]; }, $ar);}Array
(
[0] => 3
[1] => 2
[2] => 1
[3] => 7
[8] => 14
[13] => 11
)
新手,还看不懂。
很有帮助!赞一个!
mark一下,有空看下
高手,技术分真多。
我去。。。玩了1个月还不到200分的路过。
都是牛人,学习一下
learn about,thanks
好多分啊
路过一观。。。。。
学习学习
收藏…………
感谢分享,学习了
谢谢分享!还要不断学习啊!
学习了。。。。。哎 ,我们弱爆了啊
大清早就有好东西,感谢楼主
求 楼主帮助 php sql注入漏洞 不会。。

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Notepad++7.3.1
Easy-to-use and free code editor

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
