Jump to content

PHP Undefined Offset error with Multi-Dimensional Array

Hi, 

 

I am trying to create users for adding users. 

The program basically gets the username / password from a CSV file and then saves the data into an array and then puts this data into the server. 

The program is written in PHP as the server supports PHP scripts and the script does not however, it does not work in some conditions. 

 

If the array has 4 values e.g. array[0], array[1], array[2] ..... and the username / password has 0. 1, 2 ... in their name then the error I get is the following: 

PHP Notice: Undefined offset: 2 (or 3 sometimes) in (Script location)

 

The script still does work and does create the users if I run it from the command line however, this will not work if I run it from the software itself as any output (even errors) causes the script to not run. 

 

The code is below: 

 $fileName = "D:\Scripts\array.csv";$csvData = file_get_contents($fileName);$lines = explode(PHP_EOL, $csvData);$array = array();foreach ($lines as $line) {    $array[] = str_getcsv($line);} for ($row = 0; $row < count($array); $row++) {//   echo "Row number $row";for ($i = 0, $k = 1; $i < count($array), $k < count($array); $i+=2, $k+=2) {if (isset($i, $k, $row)) {         $adduser_xml ="<?xml version='1.0' encoding='utf-8'?>\n";        $adduser_xml.="<user>\n";        $adduser_xml.="<username>{$array[$row][$i]}</username>\n";        $adduser_xml.="<password>{$array[$row][$k]}</password>\n";        $adduser_xml.="<rootdir><![CDATA[D:\\test\\{$array[$row][$i]}]]></rootdir>\n"; 

The error says it's around the username, password and rootdir field.

 

Is there anything I can do to hide the errors / notices from showing up? 

 

Any help will be appreciated. 

 

Thanks

Link to post
Share on other sites

Hi, 

 

So i've done a bit of research and apparently PHP throws out these errors because the variable has not existed before so it has to create new ones. 

So i'm guessing it's referring to: $array[$row][$i]? 

 

But why does it give out these errors only when the username / password has 0, 1 ... (if the array has those numbers too)

 

So any ideas why this could be happening? 

 

Thanks 

Link to post
Share on other sites

If the array has 4 values e.g. array[0], array[1], array[2] ..... and the username / password has 0. 1, 2 ... in their name then the error I get is the following: 

PHP Notice: Undefined offset: 2 (or 3 sometimes) in (Script location)

 

Could you provide a var_dump() of $array so I can see how it is structured? Use dummy data in the csv

Link to post
Share on other sites

It's because your inner loop is iterating by 2, which means that it will potentially point to a position that is not contained in the input array.

 

Your isset check is determining that the VARIABLES i, j, and row are set, NOT those indices in the array. you should be checking:

isset($array[$i], $array[$k], $array[$row]);
Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×