2011-05-05 153 views
3

對於此代碼我得到error.at此行$sname[] = $rs['StudentId']; PHP致命錯誤:[]運算符不支持字符串 $ sname = array(); $ i = 0;[]運算符不支持字符串

foreach($data as $rs){ 

    foreach($SchoolName as $sname){ 
//  echo $rs['SchoolName'].'=='.$sname."<br />"; 
     echo $i."<br />";     
     if($rs['SchoolName'] == $sname){ 
      $sname[] = $rs['StudentId']; 
     } 
     $i++; 
    }     
} 
+0

請告訴我問題嗎? – KingCrunch 2011-05-05 11:05:16

+0

[]運算符不支持字符串...請如何解決這個 – Allov 2011-05-05 11:05:46

+0

我假設$ rs是一個字符串,而不是一個數組。例如,用var_dump檢查$ rs的數據。 – superbly 2011-05-05 11:06:21

回答

1

WORKING DEMO

$SchoolNames = Array(10003, "Southwestern College", "National University", "Western Governors University", "Southwestern College Admissions Center - Evaluations Dept"); 
$data = array(
    0 => Array(
     'STU_MANG_fname' => "Jennifer", 
     'STU_MANG_lname' => "patel", 
     'SchoolName' => "Southwestern College Admissions Center - Evaluations Dept", 
     'ShipAddress1' => "900 Otay Lakes Road", 
     'ShipState' => "CALIFORNIA" 
    ) 
); 


foreach($data as $studen_info){ 
    foreach($SchoolNames as $id=>$school_name){ 
     if($studen_info['SchoolName'] == $school_name){ 
      $student_names[$school_name] = $id; 
      //$student_names[$school_name] = $student_info['StudentId'];; 
     } 
    } 
} 

print_r($student_names); 

有學生信息數組中,你不給我「StudentId」,所以我想你想使用的學生陣列的關鍵,如果有,其實是「StudentId 「使用我行註釋掉

+0

$ rs是一個數組,$ rs ['SchoolName'] =>「學校名稱」 – Allov 2011-05-05 11:16:55

+0

我在說的是試圖在字符串上添加一個數組項目 – mcgrailm 2011-05-05 11:17:11

+0

我想這個數組......存儲學生與學校名稱 陣列 ID( [西南大學] => 10002,2003,20225 [大學] => 10282,90281 [西部州長大學] => 156 ) – Allov 2011-05-05 11:17:32

2

在接下來的循環中,

foreach ($SchoolName as $sname) { 

你是屁股將$SchoolName的每個元素都指向$sname。然後在此行上:

$sname[] = $rs['StudentId']; 

您試圖將$sname作爲數組對待。我懷疑你有一個重複的變量名稱。

+0

我聲明:$ sname = array(); – Allov 2011-05-05 11:30:06

+0

,然後在內部循環中覆蓋它。 ;) – 2011-05-05 11:33:30

+0

我想這個數組...... ......存儲學校編號與學校名稱Array([Southwestern College] => 10002,2003,20225 [National University] => 10282,90281 [Western Governors University] => 156 ) – Allov 2011-05-05 11:34:35

0

這應該工作 - 但你必須有StudentId指數呈現$ RS陣列,太...

$data = array(
    array("SchoolName" => "Roy", "StudentId" => "1000,1001,1002"), 
    array("SchoolName" => "MIT", "StudentId" => "2000,2001,2002"), 
    array("SchoolName" => "Southwestern College", "StudentId" => "3000,3001,3002"), 
    array("SchoolName" => "National University", "StudentId" => "4000,4001,4002"), 
    array("SchoolName" => "Western Governors University", "StudentId" => "5000,5001,5002"), 
); 

$return = array(); 

foreach($data as $rs){ 
    $return[$rs['SchoolName']] = $rs['StudentId']; 
} 

print_r($return); 

現場演示:http://codepad.org/HesEO4uF

+0

$ sname = array(); \t \t \t $ k = 0; \t \t \t爲($ I = 0; $ I <計數($數據); $ I ++) \t \t \t { \t \t \t \t爲($ J = 0; $Ĵ<計數($ SchoolName); $ J ++) \t \t \t \t { \t \t \t \t \t如果($數據[$ i]於[ 'SchoolName'] == $ SchoolName [$ J]){ \t \t \t \t \t \t $ sname [$ SchoolName [$ j]] [] = intval($ data [$ i] ['StudentId']); \t \t \t \t \t} \t \t \t \t \t} \t \t \t \t \t \t \t} – Allov 2011-05-05 11:50:52

+0

我不undersantd你來自哪裏去那個陣列? – mcgrailm 2011-05-05 12:06:14

+0

我自己寫過嗎? – shadyyx 2011-05-05 12:07:41

0
$sname = array(); 
      $k=0; 
      for($i=0;$i<count($data);$i++) 
      { 
       $id_str ='';    
       for($j=0;$j<count($SchoolName);$j++) 
       {     
        if($data[$i]['SchoolName'] == $SchoolName[$j]){ 
         $id_str .= intval($data[$i]['StudentId']).","; 
        } 
        $sname[$SchoolName[$j]] = $id_str; 
       }    
      }