Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 343
  • Last Modified:

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

0
kiptieu
Asked:
kiptieu
  • 4
  • 2
  • 2
1 Solution
 
gunny051499Commented:
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

0
 
gunny051499Commented:
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

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

thanks
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
kiptieuAuthor Commented:
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
0
 
Hube02Commented:
Can you upload the file testdata.txt?
0
 
kiptieuAuthor Commented:
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);
 
   
?>
0
 
Hube02Commented:
The problem appears to be your second explode statement in the function:

$items = explode("&#183;",$value);

The actual character in the text file is ^ and &#183; is interpreted literally in this case and not converted to a ^



<?php
   function string2array($string, &$myarray){
       
      $lines = explode("\n",$string);
      foreach ($lines as $value){
         $items = explode("^",$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

0
 
kiptieuAuthor Commented:
thank you so much, you are a life saver, thank you thank you thank you
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now