2016-01-13 28 views
-4

使用PHP & codeigniter。 我下面張貼到我的PHP API 從前端提交 - >{"academicYear":"2015","subjectClassification":"Primary","subjectId":"55","teacherId":[10,16,17]}PHP如何從字符串後找到數組值

我需要在我的PHP代碼找到或打印teacherId值。 基本上我的目標是打印「HELLO」3次,如果在teacherId陣列中有3個Id。

我的代碼看起來像下面,

function subjectTeacherAllocation_post(){ 
     $data = remove_unknown_fields($this->post() ,$this->form_validation->get_field_names('subjectTeacherAllocation_post')); 
     $this->form_validation->set_data($data); 

     var_dump($data); 

     $teacherList = array($data['teacherId']); 
     echo $teacherList[0]; 
     echo array_values($teacherList); 

的var_dump輸出 - >array(3) { ["academicYear"]=> string(4) "2014" ["subjectId"]=> string(2) "55" ["teacherId"]=> array(3) { [0]=> int(9) [1]=> int(15) [2]=> int(32) } }

+0

你得到的輸出是什麼echo'teacherList [0];'''''echo array_values($ teacherList);''' – Rifky

回答

0

你是在不必要的額外陣列包裝$data['teacherId'],而不是僅僅做:

$teacherList = $data['teacherId']; 
echo $teacherList[0]; //9 

具體,產生hello×的次數,其中x是上述$teacherList陣列中的元素的數量y:

foreach($teacherList as $unused){ 
    echo 'hello'; 
}