Answer the question
In order to leave comments, you need to log in
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
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 questionAsk a Question
731 491 924 answers to any question