Jump to content

getting this error : Invalid argument supplied for foreach().....


n000bie

Recommended Posts

I am getting this error message while reading from a text file. Data is save correctly but there is error while reading.

 

Array ( [0] => Array ( [0] => ) [1] => Array ( [0] => ) [2] => Array ( [0] => ) [3] => Array ( [0] => ) )

Warning: Invalid argument supplied for foreach() in C:\wamp\www\earth\index.php on line 63

 

<?php 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";
         }
      }
   }


   function string2array($string,&$myarray){
      $lines = explode("\n",$string);
      foreach ($lines as $value){
         $items = explode("&#38;#183;",$value);
         if (sizeof($items) == 2){
            $myarray[$items[0]] = $items[1];
         }
         else if (sizeof($items) == 3){
            $myarray[$items[0]][$items[1]] = $items[2];
         }
      }
   }


if(isset($_POST['save_value']) && ($_POST['save_value']==7) ) {

$index_name = $_POST['index_name'];
$curr_point = $_POST['current_pt'];
$point_change = $_POST['points_change'];
$per_change = $_POST['per_change'];

$mydb = array($index_name, $curr_point, $point_change, $per_change);
print_r($mydb);
// Convert the array into string
   	array2string($mydb,$output,$parent);

// Store the string in a file   
   	$f1 = fopen("test.txt","w+");
   	fwrite($f1,$output);
   	fclose($f1);
}

if(isset($_POST['show_records'])) {

// Read the file back from the disk
   $f1 = fopen("test.txt","r");
   $newString = fread($f1,filesize('test.txt'));
   fclose($f1);
   
   // Convert the content back to an array
   string2array($newString, $newArray);

   // Print out the array
   foreach ($newArray as $item) {   // this is line 63 
      print_r($item);
   }
}
?>

 

Any help much appreciated.

Link to comment
Share on other sites

try to change function to

   function string2array($string,&$myarray){
      $lines = explode("\n",$string);
      foreach ($lines as $value){
         $items = explode("&#38;#183;",$value);
         if (sizeof($items) == 2){
            $myarray[$items[0]] = $items[1];
         }
         else if (sizeof($items) == 3){
            $myarray[$items[0]][$items[1]] = $items[2];
         } else echo 'error in string2array function';
      }
   }

Link to comment
Share on other sites

Thanks for your answer, but I am getting error again

 

Array ( [0] => Array ( [0] => ) [1] => Array ( [0] => ) [2] => Array ( [0] => ) [3] => Array ( [0] => ) ) error in string2array functionerror in string2array functionerror in string2array functionerror in string2array functionerror in string2array function

Link to comment
Share on other sites

try to change function to

   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];
         } else echo 'error in string2array function';
      }
   }

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.