Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将数组从php传递到javascript_Php_Javascript_Arrays - Fatal编程技术网

将数组从php传递到javascript

将数组从php传递到javascript,php,javascript,arrays,Php,Javascript,Arrays,你好 我正在尝试将几个数组从php传递到javascript。对他们中的一些人来说,这是有效的,对其他人来说则不然。我得到一个文件名数组和一个包含多个文本文件内容的数组 <?php $album="./images/text_".$benutzerLang."_album1/"; $fileArray=lsRandom("./images/album1"); $listTextArray=initTexts($album,$fileArray); $false

你好 我正在尝试将几个数组从php传递到javascript。对他们中的一些人来说,这是有效的,对其他人来说则不然。我得到一个文件名数组和一个包含多个文本文件内容的数组

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


    ?>
问题是$listTextArray。在第8行中,我可以打印整个数组$listTextArray,其中包含一些小文本文件的内容,并且可以正常工作。但在“foreach-loop”中更进一步。它不再工作了。一旦我在第二个php块中使用变量$listTextArray,我的php代码的其余部分就不再执行了。我不知道为什么它不能访问该部分的$listTextArray。因为其他数组$fileArray和$falseArray没有问题。

var textListArray=new Array(5);
但是试着使用

listTextArray
使用相同的名称,一切都会好起来

var textListArray=new Array(5);
但是试着使用

listTextArray
使用相同的名称,一切都会好起来的

一些一般建议:

如果没有错误消息,很难排除此类问题。如果在您可以看到的地方没有打印错误,请查找名为php.log、error.log或httpd.log的文件,或者询问您的服务器管理员。 尝试在阵列上使用,看看它们的结构是否有任何差异。例如,在PHP中设置数组之后:

print_r($fileArray);
print_r($listTextArray);
print_r($falseArray);
与其通过循环构造JS数组,不如尝试使用内置函数。这既简化了PHP代码,也可能在出现问题时产生更有用的错误消息:

var filesArray=<?php json_encode($fileArray) ?>;
var falseArray=<?php json_encode($falseArray) ?>;
var textListArray=<?php json_encode($listTextArray) ?>;
一些一般性建议:

如果没有错误消息,很难排除此类问题。如果在您可以看到的地方没有打印错误,请查找名为php.log、error.log或httpd.log的文件,或者询问您的服务器管理员。 尝试在阵列上使用,看看它们的结构是否有任何差异。例如,在PHP中设置数组之后:

print_r($fileArray);
print_r($listTextArray);
print_r($falseArray);
与其通过循环构造JS数组,不如尝试使用内置函数。这既简化了PHP代码,也可能在出现问题时产生更有用的错误消息:

var filesArray=<?php json_encode($fileArray) ?>;
var falseArray=<?php json_encode($falseArray) ?>;
var textListArray=<?php json_encode($listTextArray) ?>;

这个问题已经为你解决了。改用json_编码


请注意,json_encode要求您的数据是utf8编码的。但是无论如何,你应该已经这样做了。

问题已经为你解决了。改用json_编码


请注意,json_encode要求您的数据是utf8编码的。但无论如何,您应该已经这样做了。

没有不匹配的地方——JS中使用了textListArray,PHP中使用了$listTextArray。不过,让它们在不同语言之间保持一致可能有助于减少混淆-没有不匹配-JS中使用了textListArray,PHP中使用了$listTextArray。不过,让它们在不同语言之间保持一致可能有助于减少混淆-非常感谢你!问题可能在于编码,所以通过使用json_encode,它解决了问题!非常感谢你!问题可能在于编码,所以通过使用json_encode,它解决了问题!谢谢你,这就是问题所在!谢谢你,这就是问题所在!