Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.9k views
in Technique[技术] by (71.8m points)

将分类下的叶子类目放到同一个数组中变成一个二维数组要怎么实现?

这是一个分类,我需要将这个数组转化成下面的格式,怎么转换,上面的分类的叶子类目可能有3级 可能是4级


array (
  0 => 
  array (
    'cat_id' => '2610',
    'cat_name' => '优选商城办公用品',
    'parent_id' => '0',
    'grade' => '1',
  ),
  1 => 
  array (
    'cat_id' => '2611',
    'cat_name' => '办公设备',
    'parent_id' => '2610',
    'grade' => '2',
  ),
  2 => 
  array (
    'cat_id' => '2612',
    'cat_name' => '扫描仪',
    'parent_id' => '2611',
    'grade' => '3',
  ),
  3 => 
  array (
    'cat_id' => '2613',
    'cat_name' => '平板式扫描仪',
    'parent_id' => '2612',
    'grade' => '4',
  ),
  4 => 
  array (
    'cat_id' => '2614',
    'cat_name' => '便携式扫描仪',
    'parent_id' => '2612',
    'grade' => '4',
  ),
  5 => 
  array (
    'cat_id' => '2615',
    'cat_name' => '高拍仪',
    'parent_id' => '2612',
    'grade' => '4',
  ),
  6 => 
  array (
    'cat_id' => '2616',
    'cat_name' => '馈纸式扫描仪',
    'parent_id' => '2612',
    'grade' => '4',
  ),
  7 => 
  array (
    'cat_id' => '2617',
    'cat_name' => '名片扫描仪',
    'parent_id' => '2612',
    'grade' => '4',
  ),
  8 => 
  array (
    'cat_id' => '2618',
    'cat_name' => '条码扫描器',
    'parent_id' => '2612',
    'grade' => '4',
  ),
  9 => 
  array (
    'cat_id' => '2619',
    'cat_name' => '工程扫描仪',
    'parent_id' => '2612',
    'grade' => '4',
  ),
  10 => 
  array (
    'cat_id' => '2620',
    'cat_name' => '书刊扫描仪',
    'parent_id' => '2612',
    'grade' => '4',
  ),
  11 => 
  array (
    'cat_id' => '2621',
    'cat_name' => '非接触式扫描仪',
    'parent_id' => '2612',
    'grade' => '4',
  ),
  12 => 
  array (
    'cat_id' => '2622',
    'cat_name' => '扫描仪配件',
    'parent_id' => '2612',
    'grade' => '4',
  ),
  13 => 
  array (
    'cat_id' => '2623',
    'cat_name' => '条码扫描枪配件',
    'parent_id' => '2612',
    'grade' => '4',
  ),
  14 => 
  array (
    'cat_id' => '2624',
    'cat_name' => '大幅面扫描仪',
    'parent_id' => '2612',
    'grade' => '4',
  ),
  15 => 
  array (
    'cat_id' => '2627',
    'cat_name' => '分类35',
    'parent_id' => '0',
    'grade' => '1',
  ),
  16 => 
  array (
    'cat_id' => '2640',
    'cat_name' => '分类8',
    'parent_id' => '2627',
    'grade' => '2',
  ),
  17 => 
  array (
    'cat_id' => '2641',
    'cat_name' => '分类9',
    'parent_id' => '2640',
    'grade' => '3',
  ),
  18 => 
  array (
    'cat_id' => '2673',
    'cat_name' => '优选办公用品',
    'parent_id' => '0',
    'grade' => '1',
  ),
  19 => 
  array (
    'cat_id' => '2674',
    'cat_name' => '办公文具',
    'parent_id' => '2673',
    'grade' => '2',
  ),
  20 => 
  array (
    'cat_id' => '2675',
    'cat_name' => '胶装本',
    'parent_id' => '2674',
    'grade' => '3',
  ),
  21 => 
  array (
    'cat_id' => '2676',
    'cat_name' => '文具本',
    'parent_id' => '2674',
    'grade' => '3',
  ),
  22 => 
  array (
    'cat_id' => '2677',
    'cat_name' => '分类8',
    'parent_id' => '2678',
    'grade' => '2',
  ),
  23 => 
  array (
    'cat_id' => '2678',
    'cat_name' => '测试分类',
    'parent_id' => '0',
    'grade' => '1',
  ),
)

转成如下格式的数组

array (
  0 => 
  array (
    'cat_id3' => '100300010005',
    'cat_name3' => '一次性餐具',
    'cat_id2' => '10030001',
    'cat_name2' => '餐具',
    'cat_id1' => '1003',
    'cat_name1' => '生活用品',
  ),
  1 => 
  array (
    'cat_id3' => '100300010006',
    'cat_name3' => '保鲜膜/保鲜袋',
    'cat_id2' => '10030001',
    'cat_name2' => '餐具',
    'cat_id1' => '1003',
    'cat_name1' => '生活用品',
  ),
  2 => 
  array (
    'cat_id3' => '100300010001',
    'cat_name3' => '碗筷',
    'cat_id2' => '10030001',
    'cat_name2' => '餐具',
    'cat_id1' => '1003',
    'cat_name1' => '生活用品',
  ),
  3 => 
  array (
    'cat_id3' => '100300010002',
    'cat_name3' => '保温饭盒',
    'cat_id2' => '10030001',
    'cat_name2' => '餐具',
    'cat_id1' => '1003',
    'cat_name1' => '生活用品',
  ),
  4 => 
  array (
    'cat_id3' => '100300010003',
    'cat_name3' => '保鲜碗/盒',
    'cat_id2' => '10030001',
    'cat_name2' => '餐具',
    'cat_id1' => '1003',
    'cat_name1' => '生活用品',
  ),
  5 => 
  array (
    'cat_id3' => '100300010007',
    'cat_name3' => '密封/密实袋',
    'cat_id2' => '10030001',
    'cat_name2' => '餐具',
    'cat_id1' => '1003',
    'cat_name1' => '生活用品',
  ),
  6 => 
  array (
    'cat_id3' => '100300060008',
    'cat_name3' => '酱油',
    'cat_id2' => '10030006',
    'cat_name2' => '粮油调味',
    'cat_id1' => '1003',
    'cat_name1' => '生活用品',
  ),
  7 => 
  array (
    'cat_id3' => '100300060012',
    'cat_name3' => '蚝油',
    'cat_id2' => '10030006',
    'cat_name2' => '粮油调味',
    'cat_id1' => '1003',
    'cat_name1' => '生活用品',
  ),
  8 => 
  array (
    'cat_id3' => '100300060010',
    'cat_name3' => '醋',
    'cat_id2' => '10030006',
    'cat_name2' => '粮油调味',
    'cat_id1' => '1003',
    'cat_name1' => '生活用品',
  ),
  9 => 
  array (
    'cat_id3' => '100300060014',
    'cat_name3' => '酱料',
    'cat_id2' => '10030006',
    'cat_name2' => '粮油调味',
    'cat_id1' => '1003',
    'cat_name1' => '生活用品',
  ),
  10 => 
  array (
    'cat_id3' => '100300060013',
    'cat_name3' => '花椒/大料',
    'cat_id2' => '10030006',
    'cat_name2' => '粮油调味',
    'cat_id1' => '1003',
    'cat_name1' => '生活用品',
  ),
  )

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

基本需要经过 数据整理 - 计算叶子 - 获取路径 - 组装求解 四个分阶段
以下代码假定数据源为$arr,根分类id0,求取每个叶子节点到根的分类信息

筛选

$ids = [];
$hash = [];
$parent_ids = [];       
foreach ($arr as $v) {
    $hash[$v['cat_id']] = $v;
    if (!in_array($v['parent_id'], $parent_ids)) {
        $parent_ids[] = $v['parent_id'];
    }
}

叶子

$ids = array_keys($hash);
$leaveas = array_values(array_diff($ids, $parent_ids));

路径

function getPath($id, &$ids, &$hash){
    $path = [];  // 3.1 查询路径id
    do {
        $path[] = $id;
        $id = $hash[$id]['parent_id'];
    } while (in_array($id, $ids));

    $arr = []; // 3.2 转换目标信息
    while ($level = count($path)) {
        $id = array_shift($path);
        $arr['cat_id'.$level] = $id;
        $arr['cat_name'.$level] = $hash[$id]['cat_name'];
    }
    return $arr;
}

组装

$rs = array_map(function($id)use($ids,$hash){
  return getPath($id,$ids,$hash);
},$leaveas);

print_r($rs);

效果

Array
(
    [0] => Array
        (
            [cat_id4] => 2613
            [cat_name4] => 平板式扫描仪
            [cat_id3] => 2612
            [cat_name3] => 扫描仪
            [cat_id2] => 2611
            [cat_name2] => 办公设备
            [cat_id1] => 2610
            [cat_name1] => 优选商城办公用品
        )

    [1] => Array
        (
            [cat_id4] => 2614
            [cat_name4] => 便携式扫描仪
            [cat_id3] => 2612
            [cat_name3] => 扫描仪
            [cat_id2] => 2611
            [cat_name2] => 办公设备
            [cat_id1] => 2610
            [cat_name1] => 优选商城办公用品
        )

    [2] => Array
        (
            [cat_id4] => 2615
            [cat_name4] => 高拍仪
            [cat_id3] => 2612
            [cat_name3] => 扫描仪
            [cat_id2] => 2611
            [cat_name2] => 办公设备
            [cat_id1] => 2610
            [cat_name1] => 优选商城办公用品
        )

    [3] => Array
        (
            [cat_id4] => 2616
            [cat_name4] => 馈纸式扫描仪
            [cat_id3] => 2612
            [cat_name3] => 扫描仪
            [cat_id2] => 2611
            [cat_name2] => 办公设备
            [cat_id1] => 2610
            [cat_name1] => 优选商城办公用品
        )

    [4] => Array
        (
            [cat_id4] => 2617
            [cat_name4] => 名片扫描仪
            [cat_id3] => 2612
            [cat_name3] => 扫描仪
            [cat_id2] => 2611
            [cat_name2] => 办公设备
            [cat_id1] => 2610
            [cat_name1] => 优选商城办公用品
        )

    [5] => Array
        (
            [cat_id4] => 2618
            [cat_name4] => 条码扫描器
            [cat_id3] => 2612
            [cat_name3] => 扫描仪
            [cat_id2] => 2611
            [cat_name2] => 办公设备
            [cat_id1] => 2610
            [cat_name1] => 优选商城办公用品
        )

    [6] => Array
        (
            [cat_id4] => 2619
            [cat_name4] => 工程扫描仪
            [cat_id3] => 2612
            [cat_name3] => 扫描仪
            [cat_id2] => 2611
            [cat_name2] => 办公设备
            [cat_id1] => 2610
            [cat_name1] => 优选商城办公用品
        )

    [7] => Array
        (
            [cat_id4] => 2620
            [cat_name4] => 书刊扫描仪
            [cat_id3] => 2612
            [cat_name3] => 扫描仪
            [cat_id2] => 2611
            [cat_name2] => 办公设备
            [cat_id1] => 2610
            [cat_name1] => 优选商城办公用品
        )

    [8] => Array
        (
            [cat_id4] => 2621
            [cat_name4] => 非接触式扫描仪
            [cat_id3] => 2612
            [cat_name3] => 扫描仪
            [cat_id2] => 2611
            [cat_name2] => 办公设备
            [cat_id1] => 2610
            [cat_name1] => 优选商城办公用品
        )

    [9] => Array
        (
            [cat_id4] => 2622
            [cat_name4] => 扫描仪配件
            [cat_id3] => 2612
            [cat_name3] => 扫描仪
            [cat_id2] => 2611
            [cat_name2] => 办公设备
            [cat_id1] => 2610
            [cat_name1] => 优选商城办公用品
        )

    [10] => Array
        (
            [cat_id4] => 2623
            [cat_name4] => 条码扫描枪配件
            [cat_id3] => 2612
            [cat_name3] => 扫描仪
            [cat_id2] => 2611
            [cat_name2] => 办公设备
            [cat_id1] => 2610
            [cat_name1] => 优选商城办公用品
        )

    [11] => Array
        (
            [cat_id4] => 2624
            [cat_name4] => 大幅面扫描仪
            [cat_id3] => 2612
            [cat_name3] => 扫描仪
            [cat_id2] => 2611
            [cat_name2] => 办公设备
            [cat_id1] => 2610
            [cat_name1] => 优选商城办公用品
        )

    [12] => Array
        (
            [cat_id3] => 2641
            [cat_name3] => 分类9
            [catt_id2] => 2640
            [cat_name2] => 分类8
            [cat_id1] => 2627
            [cat_name1] => 分类35
        )

    [13] => Array
        (
            [cat_id3] => 2675
            [cat_name3] => 胶装本
            [cat_id2] => 2674
            [cat_name2] => 办公文具
            [cat_id1] => 2673
            [cat_name1] => 优选办公用品
        )

    [14] => Array
        (
            [cat_id3] => 2676
            [cat_name3] => 文具本
            [cat_id2] => 2674
            [cat_name2] => 办公文具
            [cat_id1] => 2673
            [cat_name1] => 优选办公用品
        )

    [15] => Array
        (
            [cat_id2] => 2677
            [cat_name2] => 分类8
            [cat_id1] => 2678
            [cat_name1] => 测试分类
        )

)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...