2016-08-13 155 views
-2

請我安排在這個格式重新排列數組遞歸PHP

[ 
    (int) 0 => [ 
    'student_id' => 'AF/2016/SCH/001', 
    'AF101' => (float) 56, 
    'AF102' => (float) 46, 
], 

數據的數組,但我需要一個可以讀取陣列和重新安排它的功能出現在格式

[ 
(int) 0 => [ 
'student_id' => 'AF/2016/SCH/001', 
'course_id' => 'AF101', 
'total' => '56', 
], 
(int) 1 => [ 
'student_id' => 'AF/2016/SCH/001', 
'course_id' => 'AF102', 
'total' => '46', 
], 
] 

在其從第一陣列產生的第二陣列,所述

'AF101' => '56' 

互換到

'course_id' => 'AF101', 
'total' => '56' 

任何貢獻和建議,歡迎

+0

你有沒有嘗試什麼嗎? –

+0

請閱讀[如何問](http://stackoverflow.com/help/how-to-ask)。 – zhon

回答

0

您可以在array_walk()內使用foreach()來完成,如下所示。測試:here

<?php 

    $arr = [ 
      [ 
       'student_id' => 'AF/2016/SCH/001', 
       'AF101' => (float) 56, 
       'AF102' => (float) 46, 
      ], 
      [ 
       'student_id' => 'AF/2016/SCH/003', 
       'AF101' => (float) 76, 
       'AF102' => (float) 39, 
      ], 
    ]; 

    function filterResult($arr, $result=[]){ 
     $walk = array_walk($arr, function($data) use(&$result){ 
      if(is_array($data)){ 
       foreach($data as $key=>$item){ 
        if(preg_match("#(^[A-Z]*?)(\d)#", $key)){ 
         $result[] = [ 
          'student_id' =>$data['student_id'], 
          'course_id'  => $key, 
          'total'   => $data[$key], 
         ]; 
        } 
       } 
      } 
     }); 
     return $result; 
    } 
    var_dump(filterResult($arr)); 

var_dump() above produces:

array (size=4) 
     0 => 
     array (size=3) 
      'student_id' => string 'AF/2016/SCH/001' (length=15) 
      'course_id' => string 'AF101' (length=5) 
      'total' => float 56 
     1 => 
     array (size=3) 
      'student_id' => string 'AF/2016/SCH/001' (length=15) 
      'course_id' => string 'AF102' (length=5) 
      'total' => float 46 
     2 => 
     array (size=3) 
      'student_id' => string 'AF/2016/SCH/003' (length=15) 
      'course_id' => string 'AF101' (length=5) 
      'total' => float 76 
     3 => 
     array (size=3) 
      'student_id' => string 'AF/2016/SCH/003' (length=15) 
      'course_id' => string 'AF102' (length=5) 
      'total' => float 39 
+0

非常感謝@Poiz像魔術一樣工作 – Saviobosco

+0

@ user3792206好吧,然後將其標記爲正確答案.... – Poiz

+0

@Poiz爲什麼你是否在正則表達式模式中首先使用了#標籤(#),並且我似乎無法找到任何地方的原因,因爲這些密鑰不包含標籤。使用Live正則表達式php測試器它不匹配,但運行代碼的神奇。總之,好的工作,你的答案比我的好得多! – feareoc

0

此代碼對你的問題,假設有沒有其他每個學生子陣列比ID在和課程,成績:

$original_array = [ 
    0 => [ 
     'student_id' => 'AF/2016/SCH/001', 
     'AF101' => 56, 
     'AF102' => 46, 
    ], 
    1 => [ 
     'student_id' => 'AF/2016/SCH/002', 
     'AF101' => 76, 
     'AF102' => 39, 
    ], 
]; 
$newarray = []; 

foreach ($original_array as $student) 
{ 
    $student_id = $student['student_id']; 
    unset($student['student_id']); 
    foreach ($student as $course_id => $score) 
    { 
     $newarray[count($newarray)] = $newarray[count($newarray)] = [ 
      'student_id' => $student_id, 
      'course_id' => $course_id, 
      'total' => $score, 
     ]; 

    } 
} 
echo '<pre>'; 
var_dump($newarray); 
+0

非常感謝您的回覆。非常感謝 – Saviobosco

+0

由於@Poiz的答案更精細,你應該考慮將它標記爲正確的答案 – feareoc