这次给大家带来PHP运用foreach转换数组步骤详解,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中文网其它相关文章!
推荐阅读:
Atas ialah kandungan terperinci PHP运用foreach转换数组步骤详解. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!