2017-02-26 299 views
4

我有一个关联的多维阵列如下面PHP阵列组和创建多维阵列

$data = array(); 
$data = Array ( 
    [0] => Array ([class] => 1styear [branch] => IT [Exam] => SEM1 [student name] => Alex [Bio] => Good Boy) 
    [1] => Array ([class] => 2ndyear [branch] => Finance [Exam] => SEM1 [student name] => Mark [Bio] => Intelligent) 
    [2] => Array ([class] => 2ndyear [branch] => IT [Exam] => SEM1 [student name] => Shaun [Bio] => Football Player) 
    [3] => Array ([class] => 1styear [branch] => Finance [Exam] => SEM2 [student name] => Mike [Bio] => Sport Player) 
    [4] => Array ([class] => 1styear [branch] => IT [Exam] => SEM2 [student name] => Martin [Bio] => Smart ) 
    [5] => Array ([class] => 1styear [branch] => IT [Exam] => SEM1 [student name] => Philip [Bio] => Programmer ) 
    ) 

我需要基于来自上述阵列类似元件上以创建新的数组。意味着我必须创建数组组。例如类元素具有重复的第一年和第二年的值。所以它会抛出一些独特的元素。然后再次类是父数组和内部类数组中应该有基于分支的数组和内部的Brance考试数组和内部的考试数组中应该有学生姓名和生物的联合数组。

所以基本上阵列应该像基于类的我不喜欢下面这是工作的罚款,但如何建立内部的

$classgroup = array(); 
    foreach($data as $inarray){ 

     $classgroup[$inarray['class']][] = $inarray; 
    } 
    $classarray = array(); 
    foreach($classgroup as $key => $value){ 
      echo $key; // output is 1styear and secondyear 
      create array like above 
    } 

阵列此

array(
    "1styear" => array(
     "IT" => array(
      "SEM1" => array(
       array(
        "student name" => "Alex", 
        "Bio" => "Good Boy" 
       ), 
       array(
        "student name" => "Philip", 
        "Bio" => "Programmer" 
       ) 
      ), 
      "SEM2" => array(
       array(
        "student name" => "Martin", 
        "Bio" => "Smart" 
       ) 
      ) 
     ) 
    ), 
    "2ndyear" => array(
     "Finance" => array(
      "SEM1" => array(
       array(
        "student name" => "Mark", 
        "Bio" => "Intelligent" 
       ) 
      ), 
      "SEM2" => array(
       array(
        "student name" => "Mike", 
        "Bio" => "Sport Player" 
       ) 
      ) 
     ) 
    ) 
); 

为了使集团--- - - - - - - - - - - - - - - - 编辑 - - - - - - - - - - ---------------

来自下面的循环

foreach($data as $array){ 
     $grouped[$array["class"]][$array["branch"]][$array["Exam"]][]=array("student name"=>$array["student name"],"Bio"=>$array["Bio"]); 
} 

我得到预期的O/P ,但如果我需要另一个O/P这样

预期的O/P

array(
    '1styear' => 
     array (
      0 => 
       array(
        'Exam' => 'SEM1', 
        'branch' => 
         array (
          0 => 'IT' 
         ), 
       ), 
      1 => 
       array(
        'Exam' => 'SEM2', 
        'branch' => 
         array (
          0 => 'IT' 
         ), 
       ), 
     ), 
    '2ndyear' => 
     array (
      0 => 
       array(
        'Exam' => 'SEM1', 
        'branch' => 
         array (
          0 => 'Finance', 
         ), 
       ), 
      1 => 
       array(
        'Exam' => 'SEM2', 
        'branch' => 
         array (
          0 => 'Finance' 
         ), 
       ) 
     ), 
) 

我尝试下面的循环,但没有得到O/P如预期

foreach($data as $array){ 
     $grouped[$array["class"]][]=array("Exam"=>$array["Exam"],"branch"=>$array["branch"]); 
} 
+0

@mickmackusa我已经提到的应该是什么O/P,看行嵌套数组 – webpic

+0

@mickmackusa“所以基本上阵列应该是这样”在上面的回答。 – webpic

+0

相关:http://stackoverflow.com/questions/38366694/group-a-multidimensional-array-by-a-key-value – mickmackusa

回答

1

单循环!

foreach($data as $array){ 
     $grouped[$array["class"]][$array["branch"]][$array["Exam"]][]=array("student name"=>$array["student name"],"Bio"=>$array["Bio"]); 
} 

$grouped生产:

Array(
    [1styear] => Array(
     [IT] => Array(
      [SEM1] => array(
       [0] => array(
        [student name] => Alex, 
        [Bio] => Good Boy 
       ), 
       [1] => array(
        [student name] => Philip, 
        [Bio] => Programmer 
       ) 
      ), 
      [SEM2] => array(
       [0] => array(
        [student name] => Martin, 
        [Bio] => Smart 
       ) 
      ) 
     ), 
     [Finance] => array(
      [SEM2] => array(
       [0] => array(
        [student name] => Mike, 
        [Bio] => Sport Player 
       ) 
      ) 
     ) 
    ), 
    [2ndyear] => array(
     [Finance] => array(
      [SEM1] => array(
       [0] => array(
        [student name] => Mark, 
        [Bio] => Intelligent 
       ) 
      ) 
     ), 
     [IT] => array(
      [SEM1] => array(
       [0] => array(
        [student name] => Shaun, 
        [Bio] => Football Player 
       ) 
      ) 
     ) 
    ) 
) 

你的跟进情况下,是更有趣/具有挑战性。我不得不摒弃一些我不常玩的功能。检查了这一点:

<?php 
$data = array ( 
    array ("class"=>"1styear","branch"=>"IT","Exam"=>"SEM1","student name"=>"Alex","Bio"=>"Good Boy"), 
    array ("class"=>"2ndyear","branch"=>"Finance","Exam"=>"SEM1","student name"=>"Mark","Bio"=>"Intelligent"), 
    array ("class"=>"2ndyear", "branch"=>"IT","Exam"=>"SEM1","student name"=>"Shaun","Bio"=>"Football Player"), 
    array ("class"=>"1styear","branch"=>"Finance","Exam"=>"SEM2","student name"=>"Mike","Bio"=>"Sport Player"), 
    array ("class"=>"1styear","branch"=>"IT","Exam"=>"SEM2","student name"=>"Martin","Bio"=>"Smart"), 
    array ("class"=>"1styear","branch"=>"IT","Exam"=>"SEM1","student name"=>"Philip","Bio"=>"Programmer" ) 
); 
$class_keys=array_unique(array_column($data,"class")); // create array of unique class values 
$Exam_keys=array_unique(array_column($data,"Exam")); // create array of unique Exam values 
foreach($class_keys as $class_key){ 
    $i=0; // "class" subarray index 
    foreach($Exam_keys as $Exam_key){ 
     $q=array("class"=>$class_key,"Exam"=>$Exam_key); // this array can have 1 or more pairs 
     // create an array only of rows where $q's key-value pairs exist 
     $qualifying_array=array_filter(
      $data, 
      function($val)use($q){ 
       if(count(array_intersect_assoc($val,$q))==count($q)){ // total pairs found = total pairs sought 
        return $val; 
       } 
      }, 
      ARRAY_FILTER_USE_BOTH 
     ); 
     foreach($qualifying_array as $qa){ // push appropriate values into array 
      $grouped2[$class_key][$i]["Exam"]=$qa["Exam"]; 
      $grouped2[$class_key][$i]["branch"][]=$qa["branch"]; 
     } 
     if(isset($grouped2[$class_key][$i]["branch"])){ // ensure no duplicate values in "branch" subarray 
      $grouped2[$class_key][$i]["branch"]=array_unique($grouped2[$class_key][$i]["branch"]); 
     } 
     ++$i; // increment the index for each "class" subarray 
    } 
} 
echo "<pre>"; 
print_r($grouped2); 
echo "</pre>"; 

输出不等于你的要求是什么,但我认为你只是显示它应该是什么样子一般。如果这不太对,请告诉我。

array(
    [1styear]=>array(
     [0]=>array(
      [Exam]=>SEM1 
      [branch]=>array(
       [0]=>IT 
      ) 
     ), 
     [1]=>array(
      [Exam]=>SEM2 
      [branch]=>array(
       [0]=>Finance, 
       [1]=>IT 
      ) 
     ) 
    ), 
    [2ndyear]=>array(
     [0]=>array(
      [Exam]=>SEM1 
      [branch]=>array(
       [0]=>Finance, 
       [1]=>IT 
      ) 
     ) 
    ) 
) 
+0

@webpic我不确定'竞争者'是否意味着'学生的名字',但只是考虑单线结构的性质。如果你想保留联合关键字,在'[] ='前按顺序*命名它们,然后将所有索引/嵌套数组放在'='之后。如果您需要更多信息,只需询问(如果偏离这个问题太多了,您可能需要另外提问。无论哪种方式,我很乐意伸出援手。 – mickmackusa

+0

我编辑了这个问题看起来预期o/p – webpic

+0

多数民众赞成brilliance tnx。预计o/p。如果我只想在分支数组中使用array('finance','IT')而不是数组([0] => finance,[1] => IT)。 – webpic

0

也许像这样(未测试)?

$newData = []; 

foreach ($data as $row) { 
    $student = [ 
     'student name' => $row['student name'], 
     'Bio' => $row['Bio'] 
    ]; 
    $newData[$row['class']][$row['branch']][$row['exam']][] = $student; 
}