Re: Re: Illegal string offset error in array

From: Date: Mon, 28 Mar 2005 15:24:36 +0000
Subject: Re: Re: Illegal string offset error in array
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-211696@lists.php.net to get a copy of this message
Hello Jasper and Rasmus, thanks for reply! I have found out why this does not work. Take a look below. On Monday 28 March 2005 02:33, Rasmus Lerdorf wrote: > Jasper Bryant-Greene wrote: > > Rasmus Lerdorf wrote: > >>>> Jasper Bryant-Greene wrote: > >>>>> You can't access string offsets with square brackets [] in PHP5. > >>>>> You need to use curly braces {} instead. I have replaced all square brackets with curly braces when using string offsets. This didn't helped but worked on PHP4 and 5. > > Yes, but he's talking about PHP5. He probably has E_STRICT on, which is > > why he's getting that error. > > > > I was telling him to use the correct syntax, which will cause him to not > > get that error. I have E_STRICT on but the same behaviour with E_STRICT off > I know we are talking about PHP5, and that syntax does not throw an > E_STRICT error. > Right!! Okay, lets see my original function again (copied from first post): function getNavigationContent($content_path) {     $i = 0;     $dir = opendir($content_path);     while ($file = readdir ($dir))    {         if ($file != "." && $file != ".." && !in_array($file, explode(',', $this->arrConf['default_filtered_files']))) {             $arrAvailableContent[$i] = $file;             if(is_dir($content_path . $file)) {                 echo $n = 0;                 $subdir = opendir($content_path . $file);                 while ($subfile = readdir ($subdir))  {                     if ($subfile != "." && $subfile != "..") {                                             /*                          * This line does not work in PHP5 cause off an illegal string offset in an array ERROR                          */                         $arrAvailableContent[$i]['submenu'][$n]['file'] = $subfile;                     $n++;                     }                 }                 closedir($subdir);             }         $i++;         }     }     closedir($dir);     return $arrAvailableContent; } In the code take a look at the following line: $arrAvailableContent[$i] = $file; I am assiging a string to the var $arrAvailableContent[$i] . Now lets take a look at the line i thought which was wrong: $arrAvailableContent[$i]['submenu'][$n]['file'] = $subfile; Now i am assigning a string to $arrAvailableContent[$i]['submenu'][$n]['file'] and this is what doesn't work. Now i have var_dump'ed all the arrays and i found out, that there are no values in $arrAvailableContent[$i]['submenu'] [$n]['file'] in PHP4. In PHP5 the script exits because of a Fatal Error. But if i set the line: $arrAvailableContent[$i] = $file; To: $arrAvailableContent[$i]['file'] = $file; Everything works fine in PHP4 and PHP5. My function now looks like this: function getNavigationContent($content_path) {     $i = 0;     $dir = opendir($content_path);     while ($file = readdir ($dir))    {         if ($file != "." && $file != ".." && !in_array($file, explode(',', $this->arrConf['default_filtered_files']))) {             $arrAvailableContent[$i]{'file'} = $file;             if(is_dir($content_path . $file)) {                 echo $n = 0;                 $subdir = opendir($content_path . $file);                 while ($subfile = readdir ($subdir))  {                     if ($subfile != "." && $subfile != "..") {                                             $arrAvailableContent[$i]{'submenu'}[$n]{'file'} = $subfile;                     $n++;                     }                 }                 closedir($subdir);             }         $i++;         }     }     closedir($dir);     return $arrAvailableContent; } This works in PHP4 (4.3.10) and PHP5 (5.0.3)! Okay, after this my array doesn't look like the application wants it but this is because i never had this problem before and it should be fixed at all. I can change it quickly in my app since it is designed very clean but i just wanted you to know my conclusion. I have developed many lines of code in PHP4 in the last years but this weekend i gave PHP5 a try. I must say i am impressed. PHP4 really is missing some features but it is great too. Thanks Rasmus for inventing this scripting language. I will become a better PHP programmer just only with the new features in PHP5! Have a nice day! Regards -- # Johannes Findeisen

« previous php.general (#211696) next »