doudeng5764 2015-03-07 08:19
浏览 51
已采纳

json_encode从php数组返回null

I have an array in php from a recursiveiteratoriterator, It loads fine when I dump the file. When I try to json_encode it and pass it to the browser I get 2 scenarios:

I echo the json_ encode and get the following:

["\/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:

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.

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.

  • 写回答

1条回答 默认 最新

  • dongxiaoke2018 2015-03-07 10:51
    关注

    (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. Just leave them out.

    Also $files[] .= $pathname; looks weird. $files[] = ... appends to an array, and .= appends to a string. Doing both at the same time makes no sense. PHP seems to ignore the .=. 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?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码