# 数组操作系列之循环去重

xiaoxiao2021-02-28  8

<?php        \$where = '  1 GROUP BY `city`';        \$data = Store::getList(\$where);        if (\$data) {            \$thisgroup = '';            \$k = -1;            for (\$j = 0; \$j < count(\$data); \$j++) {                if (\$data[\$j]['province'] != \$thisgroup) {                    \$k++;                    \$temp1[\$k]['id'] = \$data[\$j]['province'];                    \$temp1[\$k]['province'] = Area::Redis_getField(\$data[\$j]['province']);                    \$temp1[\$k]['select'] = 0;                    if (\$province_id == \$data[\$j]['province']) {                        \$temp1[\$k]['select'] = 1;                    }                    \$thisgroup = \$data[\$j]['province'];                    \$act = "new";                } else {                    \$act = "merge";                }                \$select = 0;                if (\$city_id == \$data[\$j]['city']) {                    \$select = 1;                }                if (\$act == "new") {                    \$m = 0;                    \$temp1[\$k]['data'][\$m] = [                        'id' => \$data[\$j]['city'],                        'name' => Area::Redis_getField(\$data[\$j]['city']),                        'select' => \$select                    ];                    \$m++;                } else {                    \$temp1[\$k]['data'][\$m] = [                        'id' => \$data[\$j]['city'],                        'name' => Area::Redis_getField(\$data[\$j]['city']),                        'select' => \$select                    ];                    \$m++;                }                array_push(\$temp1[\$k]['data'], ['id' => 0, 'name' => '全部', 'select' => \$city_id == 0 ? 1 : 0]);            }            array_push(\$temp1, ['id' => 0, 'province' => '全部', 'select' => \$province_id == 0 ? 1 : 0, 'data' => [['id' => 0, 'name' => '全部', 'select' => \$province_id == 0 ? 1 : 0]]]);            \$id = array();            foreach (\$temp1 as \$v) {                \$id[] = \$v['id'];            }            array_multisort(\$id, SORT_ASC, \$temp1);            foreach (\$temp1 as \$key => \$val) {                \$t = \$val['data'];                \$cid = array();                foreach (\$t as \$v) {                    \$cid[] = \$v['id'];                } //                array_multisort() 函数返回排序数组。您可以输入一个或多个数组。函数先对第一个数组进行排序，接着是其他数组，如果两个或多个值相同，它将对下一个数组进行排序。 // 注释：字符串键名将被保留，但是数字键名将被重新索引，从 0 开始，并以 1 递增。 // 注释：您可以在每个数组后设置排序顺序和排序类型参数。如果没有设置，每个数组参数会使用默认值。               array_multisort(\$cid, SORT_ASC, \$t);                \$temp1[\$key]['data'] = \$t;            }        }