0

问题

我一直在尝试从单个数组构建一个新数组,我需要在其中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 级。我看不到它的第 3 级工作:(

大批

 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',
  )
4

0 回答 0