[] operator not supported for strings

17,678

Solution 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);

there was no 'StudentId' in the student info array you gave me so I assume you want to use the key of the student array if there is in fact a 'StudentId' use line i commented out

Solution 2

In the following loop,

foreach ($SchoolName as $sname) {

You are assigning each element of $SchoolName to $sname. Then on this line:

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

You attempt to treat $sname as an array. I suspect you have a duplicate variable name.

Share:
17,678
Allov
Author by

Allov

"please delete me"

Updated on June 04, 2022

Comments

  • Allov
    Allov almost 2 years

    For this code i get the error.at this line $sname[] = $rs['StudentId']; PHP Fatal error: [] operator not supported for strings $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++;  
        }                   
    }