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\st ringtoarra y.php on line 23"
"Warning: Invalid argument supplied for foreach() in C:\wamp\www\filereading\st
<?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);
// Print out the array
foreach($newArray as $item) {
echo 'Name: '.$item['name'].'<br/>';
echo 'City: '.$item['city'].'<br/>';
echo 'Age: '. $item['age'].'<br/>';
}
?>
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
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/>';
}
?>
ASKER
when i added that it doesn't seem to be reading the file, i guess my function is the problem
thanks
thanks
ASKER
Tested it got the following error:
Notice: Undefined variable: newArray in C:\wamp\www\filereading\te st.php on line 9
Warning: Invalid argument supplied for foreach() in C:\wamp\www\filereading\te st.php on line 9
Notice: Undefined variable: newArray in C:\wamp\www\filereading\te
Warning: Invalid argument supplied for foreach() in C:\wamp\www\filereading\te
Can you upload the file testdata.txt?
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,&$ou tput,&$par entkey){
foreach($myarray as $key=>$value){
if (is_array($value)) {
$parentkey .= $key."^";
array2string($value,$outpu t,$parentk ey);
$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);
?>
<?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,&$ou
foreach($myarray as $key=>$value){
if (is_array($value)) {
$parentkey .= $key."^";
array2string($value,$outpu
$parentkey = "";
}
else {
$output .= $parentkey.$key."^".$value
}
}
}
// Convert the array into string
array2string($mydb,$output
// Store the string in a file
$f1 = fopen("testdata.txt","w+")
fwrite($f1,$output);
fclose($f1);
?>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thank you so much, you are a life saver, thank you thank you thank you
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
Open in new window