function a(){ $a = array( 0 => array('id' => '22', 'type' => '1', 'style' => '1', 'number' => '15261', 'phone_id' => '1', 'phone_number' => '4008285599', 'extension_special' => NULL,), 1 => array('id' => '22', 'type' => '1', 'style' => '2', 'number' => '66002', 'phone_id' => '1', 'phone_number' => '4008285599', 'extension_special' => '1',), 14 => array('id' => '22', 'type' => '1','style' => '4', 'number' => '56431', 'phone_id' => '1', 'phone_number' => '4008285599', 'extension_special' => NULL,), 2 => array('id' => '24', 'type' => '1', 'style' => '1', 'number' => '54876', 'phone_id' => '1', 'phone_number' => '4008285599', 'extension_special' => '1',), 3 => array('id' => '25', 'type' => '1', 'style' => '1', 'number' => '07143', 'phone_id' => '1', 'phone_number' => '4008285599', 'extension_special' => NULL,), 13 => array('id' => '24', 'type' => '1', 'style' => '0', 'number' => '96949', 'phone_id' => '1', 'phone_number' => '4008285599', 'extension_special' => NULL,), 4 => array('id' => '26', 'type' => '1', 'style' => '1', 'number' => '94755', 'phone_id' => '1', 'phone_number' => '4008285599', 'extension_special' => NULL,), ); $target = array(); $temp = array(); foreach ($a as $val) { if (!empty($target)) { //$val['id']在 array_keys($target)中存在 if (in_array($val['id'], array_keys($target))) { if (!empty($temp[$val['id']])) { $target[$val['id']] = null; $target[$val['id']][$temp[$val['id']]['style']] = $temp[$val['id']]; $temp[$val['id']] = null; } $target[$val['id']][$val['style']] = $val; } else { $temp[$val['id']] = $val; $target[$val['id']] = $val; } } else { $temp[$val['id']] = $val; $target[$val['id']] = $val; } } var_dump($target); }
结果:
array (size=4) 22 => array (size=3) 1 => array (size=7) 'id' => string '22' (length=2) 'type' => string '1' (length=1) 'style' => string '1' (length=1) 'number' => string '15261' (length=5) 'phone_id' => string '1' (length=1) 'phone_number' => string '4008285599' (length=10) 'extension_special' => null 2 => array (size=7) 'id' => string '22' (length=2) 'type' => string '1' (length=1) 'style' => string '2' (length=1) 'number' => string '66002' (length=5) 'phone_id' => string '1' (length=1) 'phone_number' => string '4008285599' (length=10) 'extension_special' => string '1' (length=1) 4 => array (size=7) 'id' => string '22' (length=2) 'type' => string '1' (length=1) 'style' => string '4' (length=1) 'number' => string '56431' (length=5) 'phone_id' => string '1' (length=1) 'phone_number' => string '4008285599' (length=10) 'extension_special' => null 24 => array (size=2) 1 => array (size=7) 'id' => string '24' (length=2) 'type' => string '1' (length=1) 'style' => string '1' (length=1) 'number' => string '54876' (length=5) 'phone_id' => string '1' (length=1) 'phone_number' => string '4008285599' (length=10) 'extension_special' => string '1' (length=1) 0 => array (size=7) 'id' => string '24' (length=2) 'type' => string '1' (length=1) 'style' => string '0' (length=1) 'number' => string '96949' (length=5) 'phone_id' => string '1' (length=1) 'phone_number' => string '4008285599' (length=10) 'extension_special' => null 25 => array (size=7) 'id' => string '25' (length=2) 'type' => string '1' (length=1) 'style' => string '1' (length=1) 'number' => string '07143' (length=5) 'phone_id' => string '1' (length=1) 'phone_number' => string '4008285599' (length=10) 'extension_special' => null 26 => array (size=7) 'id' => string '26' (length=2) 'type' => string '1' (length=1) 'style' => string '1' (length=1) 'number' => string '94755' (length=5) 'phone_id' => string '1' (length=1) 'phone_number' => string '4008285599' (length=10) 'extension_special' => null
如果不想要生成的二维数组的key值,或者说再精简些,可以这样
function b(){ $a = array( 0 => array('id' => '22', 'type' => '1', 'style' => '1', 'number' => '15261', 'phone_id' => '1', 'phone_number' => '4008285599', 'extension_special' => NULL,), 1 => array('id' => '22', 'type' => '1', 'style' => '2', 'number' => '66002', 'phone_id' => '1', 'phone_number' => '4008285599', 'extension_special' => '1',), 14 => array('id' => '22', 'type' => '1','style' => '4', 'number' => '56431', 'phone_id' => '1', 'phone_number' => '4008285599', 'extension_special' => NULL,), 2 => array('id' => '24', 'type' => '1', 'style' => '1', 'number' => '54876', 'phone_id' => '1', 'phone_number' => '4008285599', 'extension_special' => '1',), 3 => array('id' => '25', 'type' => '1', 'style' => '1', 'number' => '07143', 'phone_id' => '1', 'phone_number' => '4008285599', 'extension_special' => NULL,), 13 => array('id' => '24', 'type' => '1', 'style' => '0', 'number' => '96949', 'phone_id' => '1', 'phone_number' => '4008285599', 'extension_special' => NULL,), 4 => array('id' => '26', 'type' => '1', 'style' => '1', 'number' => '94755', 'phone_id' => '1', 'phone_number' => '4008285599', 'extension_special' => NULL,), ); $target = array(); $temp = array(); foreach ($a as $val) { if (!empty($target)) { if (in_array($val['id'], array_keys($target))) { if (!empty($temp[$val['id']])) { $target[$val['id']] = null; $target[$val['id']][] = $temp[$val['id']]; $temp[$val['id']] = null; } $target[$val['id']][] = $val; } else { $temp[$val['id']] = $val; $target[$val['id']] = $val; } } else { $temp[$val['id']] = $val; $target[$val['id']] = $val; } } var_dump($target); }
结果:
array (size=4) 22 => array (size=3) 0 => array (size=7) 'id' => string '22' (length=2) 'type' => string '1' (length=1) 'style' => string '1' (length=1) 'number' => string '15261' (length=5) 'phone_id' => string '1' (length=1) 'phone_number' => string '4008285599' (length=10) 'extension_special' => null 1 => array (size=7) 'id' => string '22' (length=2) 'type' => string '1' (length=1) 'style' => string '2' (length=1) 'number' => string '66002' (length=5) 'phone_id' => string '1' (length=1) 'phone_number' => string '4008285599' (length=10) 'extension_special' => string '1' (length=1) 2 => array (size=7) 'id' => string '22' (length=2) 'type' => string '1' (length=1) 'style' => string '4' (length=1) 'number' => string '56431' (length=5) 'phone_id' => string '1' (length=1) 'phone_number' => string '4008285599' (length=10) 'extension_special' => null 24 => array (size=2) 0 => array (size=7) 'id' => string '24' (length=2) 'type' => string '1' (length=1) 'style' => string '1' (length=1) 'number' => string '54876' (length=5) 'phone_id' => string '1' (length=1) 'phone_number' => string '4008285599' (length=10) 'extension_special' => string '1' (length=1) 1 => array (size=7) 'id' => string '24' (length=2) 'type' => string '1' (length=1) 'style' => string '0' (length=1) 'number' => string '96949' (length=5) 'phone_id' => string '1' (length=1) 'phone_number' => string '4008285599' (length=10) 'extension_special' => null 25 => array (size=7) 'id' => string '25' (length=2) 'type' => string '1' (length=1) 'style' => string '1' (length=1) 'number' => string '07143' (length=5) 'phone_id' => string '1' (length=1) 'phone_number' => string '4008285599' (length=10) 'extension_special' => null 26 => array (size=7) 'id' => string '26' (length=2) 'type' => string '1' (length=1) 'style' => string '1' (length=1) 'number' => string '94755' (length=5) 'phone_id' => string '1' (length=1) 'phone_number' => string '4008285599' (length=10) 'extension_special' => null