内容简介:下面小编就为大家分享一篇PHP运用foreach神奇的转换数组(实例讲解),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
要求:
将二维数组$arr转换为以‘time'和‘type'为下标、‘data‘为值的二维数组;
原数组:
$arr = array( 0 => array( 'data' => 100, 'type' => 1, 'time' => '2018-01-26', ), 1 => array( 'data' => 200, 'type' => 2, 'time' => '2018-01-26', ), 2 => array( 'data' => 300, 'type' => 2, 'time' => '2018-01-27', ), 3 => array( 'data' => 400, 'type' => 3, 'time' => '2018-01-27', ), 4 => array( 'data' => 500, 'type' => 4, 'time' => '2018-01-28', ), );
转换:
foreach ($arr as $key => $value) { $change[$value['time']][$value['type']] = $value['data']; }
结果:
array(3) { ["2018-01-26"] => array(2) { [1] => int(100) [2] => int(200) } ["2018-01-27"] => array(2) { [2] => int(300) [3] => int(400) } ["2018-01-28"] => array(1) { [4] => int(500) } }
以上所述就是小编给大家介绍的《PHP运用foreach神奇的转换数组(实例讲解)》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Practical Algorithms for Programmers
Andrew Binstock、John Rex / Addison-Wesley Professional / 1995-06-29 / USD 39.99
Most algorithm books today are either academic textbooks or rehashes of the same tired set of algorithms. Practical Algorithms for Programmers is the first book to give complete code implementations o......一起来看看 《Practical Algorithms for Programmers》 这本书的介绍吧!