H
H
hooliganus2018-03-11 16:02:51
PHP
hooliganus, 2018-03-11 16:02:51

Why is only the last element returned?

I get a list of files. I want to return them in JSON , but only the last element is returned.

<?
    $files = array_diff(scandir("plug/lol/"), ['.','..']);
    $array = [];
    $lole = [
    "count" => count($files)
    ];
    //echo json_encode($lole);
    foreach($files as $key=>$lol){
        $array = [
    "response" => [
    "count" => 16,
    "items" =>  [
    [
     "id"=>"$lol",
            "is_dir"=> "true"    
    ],
    ] ]];

    }
    print_r($array);
    ?>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
ipokos, 2018-03-11
@ipokos

I want to return them in JSON , but only the last element is returned.

Formulate normally the question of what you want to get as a result of the cycle.
ps
if you're only assigning or outputting a variable, it doesn't need to be enclosed in double quotes.
"id"=>$lol,

R
Ruslan Ruslanov, 2019-05-29
@dasauser

heh, a year has passed, and still no answer.
did you figure out what the problem is?

foreach($files as $key=>$lol){
//$array = [//это неправильно
    $array[] = [ //это правильно
    "response" => [
    "count" => 16,
    "items" =>  [
    [
     "id"=>"$lol",
            "is_dir"=> "true"    
    ],
    ] ]];

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question