JavaScript and PHP one-dimensional arrays and two-dimensional arrays of mutual transfer

Source: Internet
Author: User

First, PHP:
  1. 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"}}}
  2. 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:
  3. 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 '}]}   
  4. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.