简体   繁体   English

json_encode从php数组返回null

[英]json_encode returning null from php array

I have an array in php from a recursiveiteratoriterator, It loads fine when I dump the file. 我有一个来自recursiveiteratoriterator的PHP数组,它在我转储文件时加载正常。 When I try to json_encode it and pass it to the browser I get 2 scenarios: 当我尝试json_encode并将其传递给浏览器时,我得到2个场景:

I echo the json_ encode and get the following: 我回应json_编码并获得以下内容:

["\/var\/www\/html\/dev\/workspace\/ftpscript\/test\/10MB.zip",
"\/var\/www\/html\/dev\/workspace\/ftpscript\/test\/test.php"]null

If I call it from a remote PHP script (using 2 servers, source (where the script is) and the one taking the info and processing it) from the remote server and json_decode it echo yields: 如果我从一个远程PHP脚本(使用2个服务器,源(脚本所在的位置)和获取信息并处理它的那个)从远程服务器和json_decode调用它,它回显产生:

NULL

The script: 剧本:

$files = array();
    $startpath = getcwd()."/test/";
    $fileinfos = new RecursiveIteratorIterator(
        new RecursiveDirectoryIterator($startpath)
    );
    foreach($fileinfos as $pathname => $fileinfo) {
        if (!$fileinfo->isFile()) continue;{
        $files[] .= $pathname;
        }
    }
    $utfEncoded = array_map("utf8_encode", $files );
    echo json_encode($utfEncoded);

I have tried with and without encoding it and it yields the same result. 我尝试过编码和不编码它会产生相同的结果。 Am I missing something? 我错过了什么吗? If it makes a difference I am running php 5.4.38 on apache. 如果它有所作为我在apache上运行php 5.4.38。

Edit: 编辑:

I have changed the iteraor around a bit and still yeild the same results... I now have: 我已经改变了一点迭代器,但仍然有相同的结果...我现在有:

function ft_srcscan() {
    $path = getcwd();
    $directory = new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS);
    $iterator = new RecursiveIteratorIterator($directory);
    $files = array();
    foreach ($iterator as $info) {
        $files[$info->getPathname()] = $info->getSize();
    }
    echo json_encode($files);
}

When I put the exact same iterator in a test page by itself along with the encode I get the results here 当我把完全相同的迭代器在测试页面本身与编码一起,我得到的结果在这里

But when I call it using the method in my class (and it is the only thing running) is here . 但是当我在我的类中使用该方法调用它时(并且它是唯一运行的)就在这里 Ultimately I am trying to get a list of files and file sizes as an array the file being the key and the size being the value. 最终,我试图获取文件列表和文件大小作为数组,文件是关键,大小是值。

(More a comment that an answer, but I need more space). (更多评论说答案,但我需要更多空间)。

The braces in the expression, that Sverri mentions do nothing and are just confusing. 表达中的括号,Sverri提到什么都不做,只是令人困惑。 Just leave them out. 把它们留下来吧。

Also $files[] .= $pathname; 另外$files[] .= $pathname; looks weird. 看起来很奇怪。 $files[] = ... appends to an array, and .= appends to a string. $files[] = ...附加到数组,而.=附加到字符串。 Doing both at the same time makes no sense. 同时做两件事都没有意义。 PHP seems to ignore the .= . PHP似乎忽略了.= Take it out, too. 把它拿出来。

So 所以

if (!$fileinfo->isFile()) continue;{
$files[] .= $pathname;
}

becomes

if (!$fileinfo->isFile()) continue;
$files[] = $pathname;

This won't help with your problem most likely. 这最有可能对您的问题没有帮助。 Does the array contain the correct data? 数组是否包含正确的数据?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM