如何将php数组回显到jquery$。每个方法

如何将php数组回显到jquery$。每个方法,jquery,Jquery,如何将php数组回送到jquery$。每个函数 以下是我迄今为止在php中尝试的内容 $index = 0; while(!feof($file)) { $singleLine = fgets($file); $totalLines[$index] = $singleLine;

如何将php数组回送到jquery$。每个函数

以下是我迄今为止在php中尝试的内容

                  $index = 0;

                  while(!feof($file))
                  {
                     $singleLine = fgets($file);
                     $totalLines[$index] = $singleLine;

                     ++$index;
                  }
                  echo $totalLines;
下面是我在jquery中阅读它的方式

       $.post("sendFile.php",{fileName: sendFileName}, function(data)
       {
              $.each(data, function(index, value)
              {
                        alert(value);
              });
       });
输出为我提供数组…当它应该为每个数组元素提供Test1、Test2、Test3时…

用于对数据进行编码

echo json_encode($totalLines);
您的php代码可以简单地编写如下:(使用函数将整个文件读入数组)


试试$.ajax,通过json发送数据,也可以通过json获取数据,我认为这是可行的you@mayank-swami-$.post只是围绕$.ajax进行包装,所以$.post很好。它只需要第四个参数,格式。例如:$.post('file.php',{field:value},函数(数据){..},'json');然后它会被解析回json不知道为什么,但json解决了我的问题,谢谢你的解决方案。
echo json_encode(file($file));
// or if need to skip the empty line and new line char
echo json_encode(file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES));