I’m trying to make a list from a bidimensional array, showing both keys and the value in that position in each line. This is the code I’ve made.
$array1=array(...);
$array2=array(...);
(...)
$array25=array(...); //this part works fine so I'm not posting all of it
$bigarray['array1']=$array1;
$bigarray['array2']=$array2;
//and so on. This also works as it should
$matrix=file(mylist.txt); //this file holds all the keys I want to print, with the format key1##key2
function printthelist($array)
{
global $bigarray,$key1,key2;
$line=explode("##",$array);
$key1=$line[0];
$key2=$line[1];
echo 'Column:'.$key1.rtrim($key2).$bigarray[$key1][rtrim($key2)].'<br/>';
}
array_walk($matrix,'printthelist');
Both keys print correctly, but when I try to get the array values printed they just don’t show. I’ve tried fixed values instead of variables as keys and it worked, both inside and outside the function. I must be missing something, I just don’t know what.
Since you asked, here’s a sample of one of the arrays:
$ib=array();
$ib[4]='Diagnostics in hospitals';
$ib[5]='False positives';
$ib[6]='Risk Factors';
$ib[7]='Protect yourself from infections';
And this is the content of the file which holds the keys to print:
other##16
invitro##9
ib##19
invitro##8
other##13
knowmore##14
psico##10
med##23
patients##19
patients##18
other##12
I’ve found the problem. The file() function makes an array from a text file, placing every line in a position of said array. However, it does not strip the EOL charachter. This means that every value imported this way has a ‘/n’ at the end. You need to remove it with rtrim() before using this data inside any function. NOTE: this also means that the file() function always returns an array of strings, even though the file only contains numbers. The documentation for this function shows that it is possible to omit the EOL charachter by adding an extra parameter after the name of the file, however, it doesn’t work in all versions of PHP.
I’ve edited the code, now it works fine.
Thank you all for your help.