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

时间:2013-04-11 13:38:53

标签: php arrays multidimensional-array

问题

我一直在尝试从单个数组构建一个新数组,我需要在顶层获取Level 1's,然后在每个父Level 2中获取Level 1's并且然后最终在他们的父Level 3's内的所有Level 2's

定义父母之间差异的方法是它们在数组中出现的顺序。

//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

代码 目前我这样做是为了得到我需要的东西:

$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++;
}

基本上我正在做的是,当我通过构建数组I并查看它属于哪个级别时,然后我采用我们到目前为止的数组并保留它直到我们找到第一级1,一旦我们有一个级别的抓取位于数组中并将其粘贴在数组项上并将新数组插入其中。这适用于第2级到第1级。它的第三级是我看不到的工作:(

阵列

 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 个答案:

没有答案