- One-dimensional to two-dimensional:
A. Code:/*@desc:一维数组转二维数组@param data 需要转换的数组@param key 主键@return ret 转换后的数组*/function onetomore($data,$key){$ret = array();foreach($data as $v){ $arr = array(); foreach($v as $k1=>$v1){ if($k1 != $key){ $arr[$k1] = $v1; } } $ret[$v[$key]][] = $arr;}return $ret;}
B. Testing:
$data = array(array( ‘user_id‘ => 1, ‘role_id‘ => 1, ‘user_nick‘ => ‘a‘),array( ‘user_id‘ => 2, ‘role_id‘ => 1, ‘user_nick‘ => ‘b‘),array( ‘user_id‘ => 3, ‘role_id‘ => 2, ‘user_nick‘ => ‘c‘));$ret = onetomore($data,‘role_id‘);var_dump($ret);
C. Output:
array(2) {[1]=>array(2) {[0]=>array(2) { ["user_id"]=> int(1) ["user_nick"]=> string(1) "a"}[1]=>array(2) { ["user_id"]=> int(2) ["user_nick"]=> string(1) "b"}}[2]=>array(1) {[0]=>array(2) { ["user_id"]=> int(3) ["user_nick"]=> string(1) "c"}}}
- Two-dimensional turn one-dimensional:
A. Code:/*@desc:二维数组转一维数组@param data 需要转换的数组@param key 主键@return ret 转换后的数组*/function moretoone($data,$key){$ret = array();$count = 0;foreach($data as $k=>$v){ foreach($v as $v1){ $ret[$count] = $v1; $ret[$count][$key] = $k; $count ++; }}return $ret;}
B. Testing:
$data = array(1 => array( array( "user_id" => 1, "user_nick" => ‘a‘ ), array( "user_id" => 2, "user_nick" => ‘b‘ )),2 => array( array( "user_id" => 3, "user_nick" => ‘c‘ )));$ret = moretoone($data,‘role_id‘);var_dump($ret);
C. Output:
array(3) {[0]=>array(3) {["user_id"]=>int(1)["user_nick"]=>string(1) "a"["role_id"]=>int(1)}[1]=>array(3) {["user_id"]=>int(2)["user_nick"]=>string(1) "b"["role_id"]=>int(1)}[2]=>array(3) {["user_id"]=>int(3)["user_nick"]=>string(1) "c"["role_id"]=>int(2)}}
Second, javascript:
- one-dimensional to two-dimensional:
A. Code: /* @desc: one-dimensional array to two-dimensional array @param data need to convert an array of @param key primary key @return ret converted array */function Onetomore (Data,key) {var ret = {}for (var i in data) {Ret[data[i][key]] = new Array ()}for (var i-data) {var arr = {} for (Var j in da Ta[i]) {if (J! = key) {Arr[j] = data[i][j]}} ret[data[i][key]].push (arr)}return Ret}
B. Test:
var data = new Array ({' uesr_id ': 1, ' role_id ': 1, ' User_nick ': ' A ' }, {' uesr_id ': 2, ' role_id ': 1, ' User_nick ': ' B '}, {' uesr_id ': 3, ' role_id ': 2 , ' User_nick ': ' C '}) var ret = onetomore (data, ' role_id ') console.log (ret)
C. Output:
{' 1 ': [{uesr_id:1, User_nick: ' A '}, {uesr_id:2, User_nick: ' B '}], ' 2 ': [{uesr_id:3, User_nick: ' C '}]}
- Two-dimensional turn one-dimensional:
A. Code:/*@desc:二维数组转一维数组@param data 需要转换的数组@param key 主键@return ret 转换后的数组*/function moretoone(data,key){var ret = new Array()var count = 0for(var i in data){ ret[count] = {} for(var j in data[i]){ ret[count] = data[i][j] ret[count][key] = i count ++ }}return ret}
B. Testing:
var data = {1:new Array( { ‘user_id‘:1, ‘user_nick‘:‘a‘ }, { ‘user_id‘:2, ‘user_nick‘:‘b‘ } ),2:new Array( { ‘user_id‘:3, ‘user_nick‘:‘c‘ } )}var ret = moretoone(data,‘role_id‘)console.log(ret)
C. Output:
[ { user_id: 1, user_nick: ‘a‘, role_id: ‘1‘ },{ user_id: 2, user_nick: ‘b‘, role_id: ‘1‘ },{ user_id: 3, user_nick: ‘c‘, role_id: ‘2‘ } ]
JavaScript and PHP one-dimensional arrays and two-dimensional arrays of mutual transfer