很久没做这事了
散分?
从数据库读取数据,多数为一个行列整齐的二维数组,姑且叫它做矩阵数组吧
当读取后用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);}
(
[0] => 3
[1] => 2
[2] => 1
[3] => 7
[8] => 14
[13] => 11
)
新手,还看不懂。
很有帮助!赞一个!
mark一下,有空看下
高手,技术分真多。
我去。。。玩了1个月还不到200分的路过。
都是牛人,学习一下
learn about,thanks
好多分啊
路过一观。。。。。
学习学习
收藏…………
感谢分享,学习了
谢谢分享!还要不断学习啊!
学习了。。。。。哎 ,我们弱爆了啊
大清早就有好东西,感谢楼主
求 楼主帮助 php sql注入漏洞 不会。。

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

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

The enumeration function in PHP8.1 enhances the clarity and type safety of the code by defining named constants. 1) Enumerations can be integers, strings or objects, improving code readability and type safety. 2) Enumeration is based on class and supports object-oriented features such as traversal and reflection. 3) Enumeration can be used for comparison and assignment to ensure type safety. 4) Enumeration supports adding methods to implement complex logic. 5) Strict type checking and error handling can avoid common errors. 6) Enumeration reduces magic value and improves maintainability, but pay attention to performance optimization.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.
