请教PHP算法,二维数组取出一维数组中id相同为几个新的二维数组。
PHP 二维数组 算法
小弟刚学PHP不久,遇到了一个问题,请教大侠们,谢谢!二维数组如下;
$arr = array(
array('id'=>100,'value'=>100),
array('id'=>100,'value'=>130,
array('id'=>100,'value'=>140),
array('id'=>100,'value'=>150),
array('id'=>101,'value'=>1600),
array('id'=>101,'value'=>100),
array('id'=>102,'value'=>100),
array('id'=>102,'value'=>100),
array('id'=>102,'value'=>100),
array('id'=>103,'value'=>100),
array('id'=>103,'value'=>100),
array('id'=>104,'value'=>100),
array('id'=>104,'value'=>100),
array('id'=>104,'value'=>100),
array('id'=>105,'value'=>100)
);
如何拆分成下面5个二维数组:
arr1(
array('id'=>100,'value'=>100),
array('id'=>100,'value'=>130,
array('id'=>100,'value'=>140),
array('id'=>100,'value'=>150)
);
arr2(
array('id'=>101,'value'=>1600),
array('id'=>101,'value'=>100)
);
arr3(
array('id'=>102,'value'=>100),
array('id'=>102,'value'=>100),
array('id'=>102,'value'=>100)
);
arr4(
array('id'=>103,'value'=>100),
array('id'=>103,'value'=>100)
);
arr5(
array('id'=>104,'value'=>100),
array('id'=>104,'value'=>100),
array('id'=>104,'value'=>100),
array('id'=>105,'value'=>100)
);
arr5(
array('id'=>105,'value'=>100)
);
回复讨论(解决方案)
arr5(
array('id'=>104,'value'=>100),
array('id'=>104,'value'=>100),
array('id'=>104,'value'=>100),
);
arr6(
array('id'=>105,'value'=>100)
);
??
到底是6个数组还是5个数组?两个arr5里面的数据还不一样?
真心没看懂~~~能不能告诉我规律?我是没看出来你下面的6个数组的规律。。。。特别是两个arr5里面的东西直接把人整蒙圈了。。。
6个,最后一个arr5应该为:arr6
arr6(
array('id'=>105,'value'=>100)
);
Sorry,拷贝错误。
就是id相同的要放入一个二维数组中,
上面数组有6种id:100,101,102,103,104,105,要分别放入到6种不同的二维数组中。
$arr = array( array('id'=>100,'value'=>100), array('id'=>100,'value'=>130), array('id'=>100,'value'=>140), array('id'=>100,'value'=>150), array('id'=>101,'value'=>1600), array('id'=>101,'value'=>100), array('id'=>102,'value'=>100), array('id'=>102,'value'=>100), array('id'=>102,'value'=>100), array('id'=>103,'value'=>100), array('id'=>103,'value'=>100), array('id'=>104,'value'=>100), array('id'=>104,'value'=>100), array('id'=>104,'value'=>100), array('id'=>105,'value'=>100) ); foreach($arr as $key=>$val){ $newarr[$val['id']][]=array('id'=>$val['id'],'value'=>$val['value']); } $arr1=$newarr['100']; $arr2=$newarr['101']; $arr3=$newarr['102']; $arr4=$newarr['103']; $arr5=$newarr['104']; $arr6=$newarr['105'];
$arr = array( array('id'=>100,'value'=>100), array('id'=>100,'value'=>130), array('id'=>100,'value'=>140), array('id'=>100,'value'=>150), array('id'=>101,'value'=>1600), array('id'=>101,'value'=>100), array('id'=>102,'value'=>100), array('id'=>102,'value'=>100), array('id'=>102,'value'=>100), array('id'=>103,'value'=>100), array('id'=>103,'value'=>100), array('id'=>104,'value'=>100), array('id'=>104,'value'=>100), array('id'=>104,'value'=>100), array('id'=>105,'value'=>100));foreach($arr as $v) $res[$v['id']][] = $v;//print_r($res);//你可以直接使用这个数组//也可以导入变量表后访问extract(array_values($res), EXTR_PREFIX_ALL, 'arr');//如print_r($arr_3);
非常感谢ahui_lcm和xuzuning,这么简单的转换我都没有想出来,真的很惭愧。
再问下2位和各位大侠,我应该怎么样能快速提高对PHP数组操作。老是受到其它语言的数组影响。
谢谢!分数我还是要给第一个帮助我的兄弟ahui_lcm。兄弟xuzuning相对来说回答的更完美点,也非常感谢xuzuning。
//代码同上//此段代码直接运行下即可$arr = array ( array ('id' => 100, 'value' => 100 ), array ('id' => 100, 'value' => 130 ), array ('id' => 100, 'value' => 140 ), array ('id' => 100, 'value' => 150 ), array ('id' => 101, 'value' => 1600 ), array ('id' => 101, 'value' => 100 ), array ('id' => 102, 'value' => 100 ), array ('id' => 102, 'value' => 100 ), array ('id' => 102, 'value' => 100 ), array ('id' => 103, 'value' => 100 ), array ('id' => 103, 'value' => 100 ), array ('id' => 104, 'value' => 100 ), array ('id' => 104, 'value' => 100 ), array ('id' => 104, 'value' => 100 ), array ('id' => 105, 'value' => 100 ) );foreach($arr as $v) $res[$v['id']][] = $v; //my_debug($res);//你可以直接使用这个数组 //也可以导入变量表后访问extract(array_values($res), EXTR_PREFIX_ALL, 'arr'); my_debug($arr_0);my_debug($arr_1);my_debug($arr_2);my_debug($arr_3);function my_debug($v = NULL, $var_name = NULL, $method = 'dump', $backtrace = false) { $display_none = ''; if (defined ( 'FRONTPAGE' )) { $display_none = "display:none;"; } print ("<div style=\"background-color:#AAAAAA;padding:5px;border:solid 1px #FF0000;margin:5px;$display_none\">") ; $call_stack = debug_backtrace (); printf ( 'file:%s ,%s(),<font color="red">line:%s</font><br>', @$call_stack [0] ['file'], @$call_stack [1] ['function'], @$call_stack [0] ['line'] ); if (! $var_name) { //取$v在源代码中的变量名字. $v_line = file ( $call_stack [0] ['file'] ); $f_line = $v_line [$call_stack [0] ['line'] - 1]; $match = array (); preg_match ( '#my_debug.*[(].*#', $f_line, $match ); if (count ( $match )) { $var_name = $match [0]; } } if ($var_name) { printf ( 'debug: <font color="blue">%s</font>', $var_name ); } //xdebug_var_dump ( $v ); print ('<pre style="border:solid 1px #0000CC;padding:5px;">') ; if ($method == 'dump') { var_dump ( $v ); } else { print_r ( $v ); } print ("
\n"); } } print ('
my_debug($arr_0);my_debug($arr_1);my_debug($arr_2);my_debug($arr_3);my_debug($arr_4);my_debug($arr_5);/*说明把这几个打印出来就都出来了!.............my_debug是我打印数组出来效果函数!很实用的,还可以定位行...*/

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...

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.

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.
