Hello I'm trying 开发者_开发百科to pass several arrays from php to javascript. For some of them it works, for others not. I get an array of filenames and an array which contains the content of several text files.
<?php
$album="./images/text_".$benutzerLang."_album1/";
$fileArray=lsRandom("./images/album1");
$listTextArray=initTexts($album,$fileArray);
$falseArray=lsRandom("./images/album2");
print $listTextArray[0];
?>
<script language="javascript" type="text/javascript">
var filesArray=new Array(5);
var falseArray=new Array(5);
var textListArray=new Array(5);
<?php
$i=0;
foreach($fileArray as $element){
print 'filesArray['.$i.']="'.$element.'";';
$i++;
}
$i=0;
foreach($falseArray as $element){
print 'falseArray['.$i.']="'.$element.'";';
$i++;
}
$i=0;
foreach($listTextArray as $element){
print 'textListArray['.$i.']="'.$element.'";';
$i++;
}
?>
function createText(){//...
</script>
<?php
function lsRandom($foldername){
$files = array();
$returnFiles=array();
$indexes=array();
$currentPath=getcwd();
chdir($foldername);
// Get the all files and folders in the given directory.
$files = glob("*", GLOB_BRACE + GLOB_MARK);
$indexes=(array_rand($files,5));
shuffle($indexes);
foreach($indexes as $in){
$returnFiles[$in]=$files[$in];
}
chdir($currentPath);
return $returnFiles;
}
function getFileText($fileName,$path){
$filePath=''.$path.''.$fileName.'';
//$file=fopen($filePath,'r');
//$text=fread($file,filesize($filePath));
$text=file_get_contents($filePath,false);
return $text;
}
function initTexts($album, $images){
$textArray1=array();
$i=0;
foreach($images as $im){
$nameArray=explode(".",$im);
$textName=''.$nameArray[0].'.txt';
$textArray1[$i]=getFileText($textName, $album);
$i++;
}
return $textArray1;
}
?>
The problem is the $listTextArray. In the 8th row I can print the whole array $listTextArray which contains the content of some small textfiles and it works. But further down in the 'foreach - loop'. It doesn't work anymore. As soon as I use the variable $listTextArray in the second php block the rest of my php code doesn't get executed anymore. I don't know why it can not access $listTextArray at that part. Because its no problem with the other arrays $fileArray and $falseArray.
Some general advice:
- It's difficult to troubleshoot this kind of problem without the error message. If no error is being printed where you can see it, look for files named
php.log
,error.log
, orhttpd.log
or ask your server admin(s). Try using
print_r()
on your arrays to see if there's any difference in how they're structured. For example, just after setting the arrays in PHP:print_r($fileArray); print_r($listTextArray); print_r($falseArray);
Rather than constructing the JS arrays via loops, try using the built-in
json_encode()
function instead. This both simplifies your PHP code and may cause more useful error messages when there are problems:var filesArray=<?php json_encode($fileArray) ?>; var falseArray=<?php json_encode($falseArray) ?>; var textListArray=<?php json_encode($listTextArray) ?>;
you initiate
var textListArray=new Array(5);
but try to use
listTextArray
Use the same name and everything will be alright
The problem is already solved for you. Use json_encode
instead.
print('filesArray = '.json_encode($filesArray));
Note that json_encode
demands that your data is utf8 encoded. But you should do that already anyway.
精彩评论