Link to home
Start Free TrialLog in
Avatar of kiptieu
kiptieu

asked on

the foreach statment in php

the listed code is giving me this error and  i don't seem to see anything wrong with the line any help will be greatly appreciated

"Warning: Invalid argument supplied for foreach() in C:\wamp\www\filereading\stringtoarray.php on line 23"
<?php
   function string2array($string,&$myarray){
      $lines = explode("\n",$string);
      foreach ($lines as $value){
         $items = explode("&#183;",$value);
         if (sizeof($items) == 2){
            $myarray[$items[0]] = $items[1];
         }
         else if (sizeof($items) == 3){
            $myarray[$items[0]][$items[1]] = $items[2];
         }
      }
   }
   // Read the file back from the disk
   $f1 = fopen("testdata.txt","r");
   $newString = fread($f1,filesize('testdata.txt'));
   fclose($f1);
   
   // Convert the content back to an array
   string2array($newString, $newArray);
 
   // Print out the array
   foreach($newArray as $item) {
      echo 'Name: '.$item['name'].'<br/>';
      echo 'City: '.$item['city'].'<br/>';
      echo 'Age: '. $item['age'].'<br/>';
   }
?>

Open in new window

Avatar of gunny051499
gunny051499
Flag of Italy image

Hi Kiptieu,

I guess foreach is not your problem, the string2array function you implemented probably is ;-)
Just put a "print_r($newArray);" after calling string2array in order to check what your array data looks like.

Cheers

Gunny
<?php
   function string2array($string,&$myarray){
      $lines = explode("\n",$string);
      foreach ($lines as $value){
         $items = explode("&#183;",$value);
         if (sizeof($items) == 2){
            $myarray[$items[0]] = $items[1];
         }
         else if (sizeof($items) == 3){
            $myarray[$items[0]][$items[1]] = $items[2];
         }
      }
   }
   // Read the file back from the disk
   $f1 = fopen("testdata.txt","r");
   $newString = fread($f1,filesize('testdata.txt'));
   fclose($f1);
   
   // Convert the content back to an array
   string2array($newString, $newArray);
 
   // DEBUG DUMP
   print_r($newArray); 
 
   // Print out the array
   foreach($newArray as $item) {
      echo 'Name: '.$item['name'].'<br/>';
      echo 'City: '.$item['city'].'<br/>';
      echo 'Age: '. $item['age'].'<br/>';
   }
?>

Open in new window

btw. you could also try the code snippet below to show your foreach in line 23 is correct.
Maybe you should rethink how you handle the call-by-reference in the function-header and how you fill it up.

Regards

Gunny
<?php
 
   $item = array();
   $item['name'] = "A";  
   $item['city'] = "B";  
   $item['age'] = "C";  
 
   // Print out the array
   foreach($newArray as $item) {
      echo 'Name: '.$item['name'].'<br/>';
      echo 'City: '.$item['city'].'<br/>';
      echo 'Age: '. $item['age'].'<br/>';
   }
?>

Open in new window

Avatar of kiptieu
kiptieu

ASKER

when i added that it doesn't seem to be reading the file, i guess my function is the problem

thanks
Avatar of kiptieu

ASKER

Tested it got the following error:

Notice: Undefined variable: newArray in C:\wamp\www\filereading\test.php on line 9

Warning: Invalid argument supplied for foreach() in C:\wamp\www\filereading\test.php on line 9
Can you upload the file testdata.txt?
Avatar of kiptieu

ASKER

The testdata.txt file is populated using this program:

<?php
 
 // Create some test data
   $mydb[0]['name'] = "John";
   $mydb[0]['city'] = "Boston";
   $mydb[0]['age']  = "32";
   $mydb[1]['name'] = "Max";
   $mydb[1]['city'] = "London";
   $mydb[1]['age']  = "41";
   $mydb[2]['name'] = "Ann";
   $mydb[2]['city'] = "Bonn";
   $mydb[2]['age']  = "29";
   $mydb[3]['name'] = "Peter";
   $mydb[3]['city'] = "Dallas";
   $mydb[3]['age']  = "28";
   $mydb[4]['name'] = "Martin";
   $mydb[4]['city'] = "Berlin";
   $mydb[4]['age']  = "22";
 
 
   function array2string($myarray,&$output,&$parentkey){
      foreach($myarray as $key=>$value){
         if (is_array($value)) {
            $parentkey .= $key."^";
            array2string($value,$output,$parentkey);
            $parentkey = "";
         }
         else {
            $output .= $parentkey.$key."^".$value."\n";
         }
      }
   }


 
   // Convert the array into string
   array2string($mydb,$output,$parent);
 
   // Store the string in a file  
   $f1 = fopen("testdata.txt","w+");
   fwrite($f1,$output);
   fclose($f1);
 
   
?>
ASKER CERTIFIED SOLUTION
Avatar of Hube02
Hube02
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kiptieu

ASKER

thank you so much, you are a life saver, thank you thank you thank you