douwen8424 2013-04-11 13:38
浏览 27

从单级XML文件构建多维数组

Problem

I have been trying to build a new array from a single array in which i need to get Level 1's at the top level, then Level 2 in of each of their parent Level 1's and then finally all Level 3's inside of their parent Level 2's.

The way to define the difference between what their parents is is by the order in which they appear in the array.

//Array Structure currently
Level 1
Level 2
Level 3
Level 3
Level 3
Level 2
Level 3
Level 3
Level 2

//Desired Array
Level 1
    Level 2
        Level 3
        Level 3
        Level 3
    Level 2
        Level 3
        Level 3
    Level 2

Code Currently I am doing this to get what I need:

$i = 0;
$singleArray = [];
foreach ($jamie['Row'] as $value){
    $item  = 0;
    $id = $value->Cell->Data;
    $level = substr ($value->Cell[1]->Data, 6, 1);
    $name = $value->Cell[2]->Data;

    /* Start Level One */
    if($level==1){
        $singleArray[$i] = array(
            'key' => $i,
            'id'=>(string) $id,
            'level'=>$level,
            'name'=>(string)$name
        );
    };
    /* End Level One */
    /* Start Level Two */
    if($level==2){
        $reverseArray = array_reverse($singleArray);
        foreach($reverseArray as $key => $value) {
            if($value['level']==1){
                $reverseArray = $value['key'];
                break;
            }
        }

        $tempArray = array(
            'key' => $i,
            'id'=>(string) $id,
            'level'=>$level,
            'name'=>(string)$name
        );

        array_push($singleArray[$reverseArray],$tempArray);
    };
    /* ENdLevel Two */
    $i++;
}

Basically what i'm doing is, as i go through building up the array I and see which level it belongs to, then i take what we have so far of the array and reserve it until we find the first level 1, once we have a level one grab where that is in the array and stick that on the array item and insert the new array in there. This works for level 2 into level 1. Its level three that I can't see to get working :(

Array

 0 => 
  array (
    'key' => 0,
    'id' => '7',
    'level' => '1',
    'name' => 'About Me',
  ),
  1 => 
  array (
    'key' => 1,
    'id' => '78',
    'level' => '2',
    'name' => 'My Conduct at Work',
  ),
  2 => 
  array (
    'key' => 2,
    'id' => '270',
    'level' => '3',
    'name' => 'Dispute Resolution/Grievance',
  ),
  3 => 
  array (
    'key' => 3,
    'id' => 'TBC',
    'level' => '3',
    'name' => 'Learn More',
  ),
  4 => 
  array (
    'key' => 4,
    'id' => '77',
    'level' => '2',
    'name' => 'My Environment, Health & Safety',
  ),
  5 => 
  array (
    'key' => 5,
    'id' => 'TBC',
    'level' => '3',
    'name' => 'Learn More',
  ),
  6 => 
  array (
    'key' => 6,
    'id' => '238',
    'level' => '3',
    'name' => 'Policies and Guidance',
  ),
  7 => 
  array (
    'key' => 7,
    'id' => '79',
    'level' => '2',
    'name' => 'My GSK Employment Journey',
  ),
  8 => 
  array (
    'key' => 8,
    'id' => '274',
    'level' => '3',
    'name' => 'Changes in Role',
  ),
  9 => 
  array (
    'key' => 9,
    'id' => '275',
    'level' => '3',
    'name' => 'Leaving GSK',
  ),
  10 => 
  array (
    'key' => 10,
    'id' => '273',
    'level' => '3',
    'name' => 'My Moves',
  ),
  11 => 
  array (
    'key' => 11,
    'id' => '272',
    'level' => '3',
    'name' => 'Starting at GSK',
  ),
  12 => 
  array (
    'key' => 12,
    'id' => '80',
    'level' => '2',
    'name' => 'My Personal Information',
  ),
  13 => 
  array (
    'key' => 13,
    'id' => 'TBC',
    'level' => '3',
    'name' => 'Learn More',
  ),
  14 => 
  array (
    'key' => 14,
    'id' => '276',
    'level' => '3',
    'name' => 'Manage my Personal Information',
  ),
  15 => 
  array (
    'key' => 15,
    'id' => '76',
    'level' => '2',
    'name' => 'My Talent, Performance & Development',
  ),
  16 => 
  array (
    'key' => 16,
    'id' => '266',
    'level' => '3',
    'name' => 'My Development',
  ),
  17 => 
  array (
    'key' => 17,
    'id' => '268',
    'level' => '3',
    'name' => 'My Learning',
  ),
  18 => 
  array (
    'key' => 18,
    'id' => '267',
    'level' => '3',
    'name' => 'My Performance',
  ),
  19 => 
  array (
    'key' => 19,
    'id' => '75',
    'level' => '2',
    'name' => 'My TotalReward',
  ),
  20 => 
  array (
    'key' => 20,
    'id' => '287',
    'level' => '3',
    'name' => 'Learn More My Benefits',
  ),
  21 => 
  array (
    'key' => 21,
    'id' => '288',
    'level' => '3',
    'name' => 'Learn More My Pay',
  ),
  22 => 
  array (
    'key' => 22,
    'id' => '264',
    'level' => '3',
    'name' => 'My Benefits',
  ),
  23 => 
  array (
    'key' => 23,
    'id' => '263',
    'level' => '3',
    'name' => 'My Pay',
  ),
  24 => 
  array (
    'key' => 24,
    'id' => '265',
    'level' => '3',
    'name' => 'My Recognition',
  ),
  25 => 
  array (
    'key' => 25,
    'id' => '81',
    'level' => '2',
    'name' => 'Time Off, Time & Attendance',
  ),
  26 => 
  array (
    'key' => 26,
    'id' => '281',
    'level' => '3',
    'name' => 'Flexible Working',
  ),
  27 => 
  array (
    'key' => 27,
    'id' => 'TBC',
    'level' => '3',
    'name' => 'Learn More',
  ),
  28 => 
  array (
    'key' => 28,
    'id' => '279',
    'level' => '3',
    'name' => 'Leave of Absence',
  ),
  29 => 
  array (
    'key' => 29,
    'id' => '280',
    'level' => '3',
    'name' => 'Time & Attendance',
  ),
  30 => 
  array (
    'key' => 30,
    'id' => '278',
    'level' => '3',
    'name' => 'Time Off',
  )
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 无线电能传输系统MATLAB仿真问题
    • ¥50 如何用脚本实现输入法的热键设置
    • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
    • ¥30 深度学习,前后端连接
    • ¥15 孟德尔随机化结果不一致
    • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
    • ¥15 谁有desed数据集呀
    • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
    • ¥15 关于#hadoop#的问题