Group array values based on key in php?

前端 未结 4 867
囚心锁ツ
囚心锁ツ 2020-11-29 06:16

I have a array like this:

$str=
   Array
(
    [No] => 101
    [Paper_id] => WE3P-1
    [Title] => \"a1\"
    [Author] => ABC
    [Aff_list] =>         


        
4条回答
  •  臣服心动
    2020-11-29 06:36

    Thanks to crater and Fabio's answer. I updated the code to check if the size of the key is not greater than one (1), underscore will not be appended.

    function _group_by($array, $keys=array()) {
        $return = array();
        $append = (sizeof($keys) > 1 ? "_" : null);
        foreach($array as $val){
            $final_key = "";
            foreach($keys as $theKey){
                $final_key .= $val[$theKey] . $append;
            }
            $return[$final_key][] = $val;
        }
        return $return;
    }
    

提交回复
热议问题