简体   繁体   中英

Is there a better way to write the logic of this code?

I will get the $dados form Database, each element will have 3 infos cod , nome , tree .

The tree will be used to make an tree, visually just like a File Manager, to display this categories (In var $nome ) and the var $cod will be hidden.

The code already works, but I would like to know if is possible to change the "switch" for other code, mainly, because I covered only over the 7nd sub-tree, but if the tree is bigger? This code won't work.

Any ideas?

<?php

$dados[0] = [ 'cod'  => 1,
              'nome' => 'Item A',
              'tree' => '1.001'
];

$dados[1] = [ 'cod'  => 2,
              'nome' => 'Item B',
              'tree' => '1.002'
];

$dados[2] = [ 'cod'  => 3,
              'nome' => 'Sub-Item A',
              'tree' => '1.001.001'
];

foreach ( $dados as $v ) {
    $tree  = explode ( '.', $v['tree'] );
    $total = count ( $tree );
    switch ( $total ) {
        case 1:
            $data[$tree[0]] = array (
                'cod'  => $v['cod'],
                'nome' => $v['nome']
            );
            break;
        case 2:
            $data[$tree[0]][$tree[1]] = array (
                'cod'  => $v['cod'],
                'nome' => $v['nome']
            );
            break;
        case 3:
            $data[$tree[0]][$tree[1]][$tree[2]] = array (
                'cod'  => $v['cod'],
                'nome' => $v['nome']
            );
            break;
        case 4:
            $data[$tree[0]][$tree[1]][$tree[2]][$tree[3]] = array (
                'cod'  => $v['cod'],
                'nome' => $v['nome']
            );
            break;
        case 5:
            $data[$tree[0]][$tree[1]][$tree[2]][$tree[3]][$tree[4]] = array (
                'cod'  => $v['cod'],
                'nome' => $v['nome']
            );
            break;
        case 6:
            $data[$tree[0]][$tree[1]][$tree[2]][$tree[3]][$tree[4]][$tree[5]] = array (
                'cod'  => $v['cod'],
                'nome' => $v['nome']
            );
            break;
        case 7:
            $data[$tree[0]][$tree[1]][$tree[2]][$tree[3]][$tree[4]][$tree[5]][$tree[6]] = array (
                'cod'  => $v['cod'],
                'nome' => $v['nome']
            );
            break;
        default:
            break;

    }

}

var_dump ( $data );

?>
foreach ( $dados as $v ) {
    $tree  = explode ( '.', $v['tree'] );
                                         // You can think about $p as a pointer
    $p = &$data;                         // now it point to root of array 
    foreach($tree as $i) {
       if (!isset($p[$i])) $p[$i] = '';  // if item is not present, create it
       $p = &$p[$i];                     // change pointer to this item of array, 
                                         // so $p will be $data[$tree[0]], then $data[$tree[1]]...
       }                                 // path finished, this is our aim
       $p = array (                      // add array to a leaf
                'cod'  => $v['cod'],     
                'nome' => $v['nome']
           );
}

var_dump ( $data );

@splash58 beat me to it but here is my version:

foreach($dados as $v){
    $tree           = explode ( '.', $v['tree'] );
    $arrayElement   = array('cod'  => $v['cod'],'nome' => $v['nome']);
    $arrayHolder    = &$data;
    foreach($tree as $key){
        $arrayHolder[$key] = isset($arrayHolder[$key]) ? $arrayHolder[$key] : array();
        $arrayHolder = &$arrayHolder[$key];
    }
    $arrayHolder = $arrayElement;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM