'I'm trying to creat an array where I'm getting information from a json file. but the result is coming in one line

I expect to get the user and password one by one from the JSON file, but instead, the output is coming in one line.

<?php
$json = file_get_contents("URL");
$details = json_decode($json);
foreach($details->{'user'} as $output) {
    $username = $output->{'username'};
    $password = $output->{'password'};
    $LOGIN_INFORMATION = array(
        "$username" => "$password",
        'admin' => 'admin'
    );
}

I don't know how to formol my question in an easy way. when a user is writing their username and password it doesn't log in because of all users is in one line.

enter image description here



Solution 1:[1]

You need to push the data onto the array, not replace the variable each time through the loop. You do this by putting [] after the variable name in the assignment.

<?php
$json = file_get_contents("URL");
$details = json_decode($json);
$LOGIN_INFORMATION = [];
foreach($details->user as $output) {
    $username = $output->username;
    $password = $output->password;
    $LOGIN_INFORMATION[] = array(
        "$username" => $password,
        'admin' => 'admin'
    );
}
var_dump($LOGIN_INFORMATION);

DEMO

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1