首頁 >後端開發 >php教程 >php数组如何转化成如下数组...

php数组如何转化成如下数组...

WBOY
WBOY原創
2016-06-06 20:09:57794瀏覽

<code>Array
(
    [0] => Array
        (
            [ce_id] => 1
            [ce_category_id] => 1
            [ce_user_id] => 10
            [ce_remark] => 123
            [ce_img] => /upload/certification/1_1459410425_4041.png
            [ce_create_at] => 2016-03-31 15:47:05
            [ce_update_at] => 2016-03-31 15:47:05
            [cn_id] => 1
            [cn_certification_name] => 学历证明
            [cn_create_at] => 2016-03-31 10:54:02
            [cn_create_user_id] => 
        )

    [1] => Array
        (
            [ce_id] => 2
            [ce_category_id] => 1
            [ce_user_id] => 10
            [ce_remark] => 正面
            [ce_img] => /upload/certification/1_1459413288_5730.png
            [ce_create_at] => 2016-03-31 16:34:48
            [ce_update_at] => 2016-03-31 16:34:48
            [cn_id] => 1
            [cn_certification_name] => 学历证明
            [cn_create_at] => 2016-03-31 10:54:02
            [cn_create_user_id] => 
        )

    [2] => Array
        (
            [ce_id] => 3
            [ce_category_id] => 1
            [ce_user_id] => 10
            [ce_remark] => 正面
            [ce_img] => /upload/certification/1_1459413934_4724.png
            [ce_create_at] => 2016-03-31 16:45:34
            [ce_update_at] => 2016-03-31 16:45:34
            [cn_id] => 1
            [cn_certification_name] => 学历证明
            [cn_create_at] => 2016-03-31 10:54:02
            [cn_create_user_id] => 
        )

    [3] => Array
        (
            [ce_id] => 4
            [ce_category_id] => 1
            [ce_user_id] => 10
            [ce_remark] => 正面
            [ce_img] => /upload/certification/1_1459414535_2607.png
            [ce_create_at] => 2016-03-31 16:55:35
            [ce_update_at] => 2016-03-31 16:55:35
            [cn_id] => 1
            [cn_certification_name] => 学历证明a
            [cn_create_at] => 2016-03-31 10:54:02
            [cn_create_user_id] => 
        )

)
</code>

这正数组怎么变成
cn_certification_name为键,cn_certification_name这字段相同的值就放在这字段下面。。
如:

<code>array(2) {
  ["学历证明"]=>
  array(4) {
    [0]=>
    array(11) {
      ["ce_id"]=>
      string(1) "1"
      ["ce_category_id"]=>
      string(1) "1"
      ["ce_user_id"]=>
      string(2) "10"
      ["ce_remark"]=>
      string(3) "123"
      ["ce_img"]=>
      string(43) "/upload/certification/1_1459410425_4041.png"
      ["ce_create_at"]=>
      string(19) "2016-03-31 15:47:05"
      ["ce_update_at"]=>
      string(19) "2016-03-31 15:47:05"
      ["cn_id"]=>
      string(1) "1"
      ["cn_certification_name"]=>
      string(12) "学历证明"
      ["cn_create_at"]=>
      string(19) "2016-03-31 10:54:02"
      ["cn_create_user_id"]=>
      NULL
    }
    [1]=>
    array(11) {
      ["ce_id"]=>
      string(1) "2"
      ["ce_category_id"]=>
      string(1) "1"
      ["ce_user_id"]=>
      string(2) "10"
      ["ce_remark"]=>
      string(6) "正面"
      ["ce_img"]=>
      string(43) "/upload/certification/1_1459413288_5730.png"
      ["ce_create_at"]=>
      string(19) "2016-03-31 16:34:48"
      ["ce_update_at"]=>
      string(19) "2016-03-31 16:34:48"
      ["cn_id"]=>
      string(1) "1"
      ["cn_certification_name"]=>
      string(12) "学历证明"
      ["cn_create_at"]=>
      string(19) "2016-03-31 10:54:02"
      ["cn_create_user_id"]=>
      NULL
    }
    [2]=>
    array(11) {
      ["ce_id"]=>
      string(1) "3"
      ["ce_category_id"]=>
      string(1) "1"
      ["ce_user_id"]=>
      string(2) "10"
      ["ce_remark"]=>
      string(6) "正面"
      ["ce_img"]=>
      string(43) "/upload/certification/1_1459413934_4724.png"
      ["ce_create_at"]=>
      string(19) "2016-03-31 16:45:34"
      ["ce_update_at"]=>
      string(19) "2016-03-31 16:45:34"
      ["cn_id"]=>
      string(1) "1"
      ["cn_certification_name"]=>
      string(12) "学历证明"
      ["cn_create_at"]=>
      string(19) "2016-03-31 10:54:02"
      ["cn_create_user_id"]=>
      NULL
    }
  }
  ["学历证明a"]=>
  array(1) {
    [0]=>
    array(11) {
      ["ce_id"]=>
      string(2) "23"
      ["ce_category_id"]=>
      string(1) "3"
      ["ce_user_id"]=>
      string(2) "10"
      ["ce_remark"]=>
      string(3) "789"
      ["ce_img"]=>
      string(43) "/upload/certification/3_1459481224_8477.png"
      ["ce_create_at"]=>
      string(19) "2016-04-01 11:27:04"
      ["ce_update_at"]=>
      string(19) "2016-04-01 11:27:04"
      ["cn_id"]=>
      string(1) "3"
      ["cn_certification_name"]=>
      string(13) "学历证明a"
      ["cn_create_at"]=>
      string(19) "2016-04-01 11:26:11"
      ["cn_create_user_id"]=>
      NULL
    }
  }
}
</code>

回复内容:

<code>Array
(
    [0] => Array
        (
            [ce_id] => 1
            [ce_category_id] => 1
            [ce_user_id] => 10
            [ce_remark] => 123
            [ce_img] => /upload/certification/1_1459410425_4041.png
            [ce_create_at] => 2016-03-31 15:47:05
            [ce_update_at] => 2016-03-31 15:47:05
            [cn_id] => 1
            [cn_certification_name] => 学历证明
            [cn_create_at] => 2016-03-31 10:54:02
            [cn_create_user_id] => 
        )

    [1] => Array
        (
            [ce_id] => 2
            [ce_category_id] => 1
            [ce_user_id] => 10
            [ce_remark] => 正面
            [ce_img] => /upload/certification/1_1459413288_5730.png
            [ce_create_at] => 2016-03-31 16:34:48
            [ce_update_at] => 2016-03-31 16:34:48
            [cn_id] => 1
            [cn_certification_name] => 学历证明
            [cn_create_at] => 2016-03-31 10:54:02
            [cn_create_user_id] => 
        )

    [2] => Array
        (
            [ce_id] => 3
            [ce_category_id] => 1
            [ce_user_id] => 10
            [ce_remark] => 正面
            [ce_img] => /upload/certification/1_1459413934_4724.png
            [ce_create_at] => 2016-03-31 16:45:34
            [ce_update_at] => 2016-03-31 16:45:34
            [cn_id] => 1
            [cn_certification_name] => 学历证明
            [cn_create_at] => 2016-03-31 10:54:02
            [cn_create_user_id] => 
        )

    [3] => Array
        (
            [ce_id] => 4
            [ce_category_id] => 1
            [ce_user_id] => 10
            [ce_remark] => 正面
            [ce_img] => /upload/certification/1_1459414535_2607.png
            [ce_create_at] => 2016-03-31 16:55:35
            [ce_update_at] => 2016-03-31 16:55:35
            [cn_id] => 1
            [cn_certification_name] => 学历证明a
            [cn_create_at] => 2016-03-31 10:54:02
            [cn_create_user_id] => 
        )

)
</code>

这正数组怎么变成
cn_certification_name为键,cn_certification_name这字段相同的值就放在这字段下面。。
如:

<code>array(2) {
  ["学历证明"]=>
  array(4) {
    [0]=>
    array(11) {
      ["ce_id"]=>
      string(1) "1"
      ["ce_category_id"]=>
      string(1) "1"
      ["ce_user_id"]=>
      string(2) "10"
      ["ce_remark"]=>
      string(3) "123"
      ["ce_img"]=>
      string(43) "/upload/certification/1_1459410425_4041.png"
      ["ce_create_at"]=>
      string(19) "2016-03-31 15:47:05"
      ["ce_update_at"]=>
      string(19) "2016-03-31 15:47:05"
      ["cn_id"]=>
      string(1) "1"
      ["cn_certification_name"]=>
      string(12) "学历证明"
      ["cn_create_at"]=>
      string(19) "2016-03-31 10:54:02"
      ["cn_create_user_id"]=>
      NULL
    }
    [1]=>
    array(11) {
      ["ce_id"]=>
      string(1) "2"
      ["ce_category_id"]=>
      string(1) "1"
      ["ce_user_id"]=>
      string(2) "10"
      ["ce_remark"]=>
      string(6) "正面"
      ["ce_img"]=>
      string(43) "/upload/certification/1_1459413288_5730.png"
      ["ce_create_at"]=>
      string(19) "2016-03-31 16:34:48"
      ["ce_update_at"]=>
      string(19) "2016-03-31 16:34:48"
      ["cn_id"]=>
      string(1) "1"
      ["cn_certification_name"]=>
      string(12) "学历证明"
      ["cn_create_at"]=>
      string(19) "2016-03-31 10:54:02"
      ["cn_create_user_id"]=>
      NULL
    }
    [2]=>
    array(11) {
      ["ce_id"]=>
      string(1) "3"
      ["ce_category_id"]=>
      string(1) "1"
      ["ce_user_id"]=>
      string(2) "10"
      ["ce_remark"]=>
      string(6) "正面"
      ["ce_img"]=>
      string(43) "/upload/certification/1_1459413934_4724.png"
      ["ce_create_at"]=>
      string(19) "2016-03-31 16:45:34"
      ["ce_update_at"]=>
      string(19) "2016-03-31 16:45:34"
      ["cn_id"]=>
      string(1) "1"
      ["cn_certification_name"]=>
      string(12) "学历证明"
      ["cn_create_at"]=>
      string(19) "2016-03-31 10:54:02"
      ["cn_create_user_id"]=>
      NULL
    }
  }
  ["学历证明a"]=>
  array(1) {
    [0]=>
    array(11) {
      ["ce_id"]=>
      string(2) "23"
      ["ce_category_id"]=>
      string(1) "3"
      ["ce_user_id"]=>
      string(2) "10"
      ["ce_remark"]=>
      string(3) "789"
      ["ce_img"]=>
      string(43) "/upload/certification/3_1459481224_8477.png"
      ["ce_create_at"]=>
      string(19) "2016-04-01 11:27:04"
      ["ce_update_at"]=>
      string(19) "2016-04-01 11:27:04"
      ["cn_id"]=>
      string(1) "3"
      ["cn_certification_name"]=>
      string(13) "学历证明a"
      ["cn_create_at"]=>
      string(19) "2016-04-01 11:26:11"
      ["cn_create_user_id"]=>
      NULL
    }
  }
}
</code>

这应该是你想要的意思了

<code>function changeArray($arr){
        $newArr = $keyTmp =  array();
        foreach($arr as $val){
            array_push($keyTmp, $val['cn_certification_name']);
        }
        unset($val);
        $keyTmp = array_unique($keyTmp);
        foreach($arr as $val){
            foreach($keyTmp as $tmpVal){
                if($val['cn_certification_name'] == $tmpVal){
                    $newArr[$tmpVal][] = $val;
                }
            }
        }
        return $newArr;
    }</code>

<code class="php">// 来个简单的
$res = [];
foreach($arr as $v){
     $res[$v['cn_certification_name']][] = $v;
}</code>

你是要按照字段cn_certification_name排序吗?是这意思?

你是想实现二维数组转多维吧 比如爹下面有儿子儿子下面有儿子 然后这条关系都挂在同同一条数据下面

foreach($ARR as $VALUA) 这个

(strval)$;

typeof( )

用这三个函数,做不到吗?

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn