n000bie Posted December 30, 2009 Share Posted December 30, 2009 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("&#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. Quote Link to comment https://forums.phpfreaks.com/topic/186658-getting-this-error-invalid-argument-supplied-for-foreach/ Share on other sites More sharing options...
sasa Posted December 30, 2009 Share Posted December 30, 2009 try to change function to 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]; } else echo 'error in string2array function'; } } Quote Link to comment https://forums.phpfreaks.com/topic/186658-getting-this-error-invalid-argument-supplied-for-foreach/#findComment-985808 Share on other sites More sharing options...
n000bie Posted December 30, 2009 Author Share Posted December 30, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/186658-getting-this-error-invalid-argument-supplied-for-foreach/#findComment-985810 Share on other sites More sharing options...
sasa Posted December 30, 2009 Share Posted December 30, 2009 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'; } } Quote Link to comment https://forums.phpfreaks.com/topic/186658-getting-this-error-invalid-argument-supplied-for-foreach/#findComment-985873 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.