PHP教程

php根据数组内值相同组成新数组

本文主要是介绍php根据数组内值相同组成新数组,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
          
            $result= array();
            //$dataList是你要遍历的数组
            foreach ($dataList as $key => $info) {
                $result[$info['valid_period']]["day"]=$info['valid_period'];
                $result[$info['valid_period']]["array"][]=$info;
            }
            //array_values($result)这是为了去掉健值生成数字健值
             $data["dataList"] =array_values($result);
            //结果
            //认真看你就能懂了,很简单的。
            
           "dataList": [
            {
                "day": "7",
                "array": [
                    {
                        "id": "93",
                        "valid_period": "7",
                        "data": "1024.00",
                        "plan_name": "China 1GB 7days"
                    },
                    {
                        "id": "94",
                        "valid_period": "7",
                        "data": "2048.00",
                        "plan_name": "China 2GB 7days"
                    }
                ]
            },
            {
                "day": "30",
                "array": [
                    {
                        "id": "98",
                        "valid_period": "30",
                        "data": "1024.00",
                        "plan_name": "China 1GB 30days"
                    },
                    {
                        "id": "99",
                        "valid_period": "30",
                        "data": "2048.00",
                        "plan_name": "China 2GB 30days"
                    },
            
                ]
            },
            {
                "day": "90",
                "array": [
                    {
                        "id": "110",
                        "valid_period": "90",
                        "data": "10240.00",
                        "plan_name": "China 10GB 90days"
                    },
                    {
                        "id": "111",
                        "valid_period": "90",
                        "data": "15360.00",
                        "plan_name": "China 15GB 90days"
                    }
            }
        ]
    },
            
这篇关于php根据数组内值相同组成新数组的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!