X
X
Xooliganus2018-03-11 13:31:21
PHP
Xooliganus, 2018-03-11 13:31:21

Problem with json in while?

In a loop I collect files. I need to return it in JSON in this format:
{"1":"d.txt","2":"dj.txt"}
But it comes out like this:
{"1":"d.txt"}{"2":"dj.txt"}
Here is the code:

<?
$files = array_diff(scandir("plug/lol/"), ['.','..']);
$lole = [
"count" => count($files)
];
//echo json_encode($lole);
foreach($files as $key=>$lol){
    $result = [
    $key-1=> $lol
    ];
    echo json_encode($result);
}
?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
coderisimo, 2018-03-11
@coderisimo

and where do you have the while promised in the header?
why output a new array each time inside the loop, instead of creating a GENERAL one? etc. )))))
Below is a simple code that solves your problem:
sandbox.onlinephpfunctions.com/code/68dcca66ef63c3...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question