'how to create an array in php

I don't know how to build an array I don't understand it, I already have the responses from the server in json_encode but I don't know how to use the results in an "input option".

     $nun=0;
     $arch = [];
     //$file='file.fsc';
     //echo dirname(__FILE__) . './../bankreceipts/*';
     foreach (glob(dirname(__FILE__) . './../bankreceipts/*') as $filename) {
         $nun++;
         $filename = basename($filename);
         $arch[$nun] = $filename;
     }
     echo json_encode($file);

the response from the server is:

{"1":"billy.pdf","2":"mov19-03-20222202319817.csv","3":"pichincha_15_03_2022.csv"}

I need the option to have value="billy.pdf" another value="mov19-03-20222202319817.csv" etc.



Solution 1:[1]

You can parse the response as an array using json_decode with the array argument as true, then treat it like key/values:

<?php

$data = '{"1":"billy.pdf","2":"mov19-03-20222202319817.csv","3":"pichincha_15_03_2022.csv"}';
$data = json_decode($data, true);
echo 'pdf: ' . $data["1"] . "\n";
echo 'csv 1: ' . $data["2"] . "\n";
echo 'csv 2: ' . $data["3"] . "\n";

/* output

pdf: billy.pdf
csv 1: mov19-03-20222202319817.csv
csv 2: pichincha_15_03_2022.csv

*/

https://tehplayground.com/9wbYaIulzsHmokqX

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 Kinglish