<?php
/*
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Decode JSON Object in PHP</title>
</head>
<body>

*/


// https://www.tutorialrepublic.com/php-tutorial/php-json-parsing.php

// Store JSON data in a PHP variable
$json = '{"Peter":65,"Harry":80,"John":78,"Clark":90}';
 
//var_dump(json_decode($json));


$row = array (
    "id" => 18,
    "first_name" => "Dwayne",
    "last_name" => "Hicks",
    "email" => "dwaynehicks@usssulaco.com"
);

$output = json_encode($row);

//echo $output;


//https://www.akawebdesign.com/2009/08/18/json-formatting-in-php/

	$val1 = array();
	$val1[0] = "1";
	$val1[1] = "2";
	$val1[2] = "3";
	$val1[3] = "4";
	
	$val2 = array();
	$val2[0] = "un";
	$val2[1] = "deux";
	$val2[2] = "trois";
	$val2[3] = "quatre";
	
	$val3 = array();
	$val3[0] = "aaa";
	$val3[1] = "bbb";
	$val3[2] = "ccc";
	$val3[3] = "ddd";

	$json_toto = array("user" => array() );
	
	for ($i = 0; $i < sizeof($val1); $i++) 
	{
		$json_row = array(
						"id" => $val1[$i],
						"nom" => $val2[$i],
						"prenom" => $val3[$i]
						);
					 array_push($json_toto["user"], $json_row );
	}				 

	
	$tab = json_encode($json_toto);
	
//	echo $tab;
	
	
	$return_array = array();

	if ($handle = opendir('.')) 
	{
		$file_name = array();
		$file_date = array();
		$file_size = array();
	
		$i = 0;

		while (false !== ($file = readdir($handle))) 
		{
			if($file == "." or $file == "..")
			{
			}
			else
			{
				$file_name[$i] = $file;
				$file_date[$i] = date ("d-m-Y|H:i:s", filemtime($file) );
				$file_size[$i] = human_filesize(filesize($file), 2); // filesize($file);
				$i = $i + 1;
			}
		}

/*		
		echo "<br>";
		echo "<pre>";
		print_r($file_name);
		print_r($file_date);
		print_r($file_size);
*/		
	
		$json_infos = array("MP3_file" => array() );
	
		for ($i = 0; $i < sizeof($file_name); $i++) 
		{
			$json_ligne = array(
								"name" => $file_name[$i],
								"date" => $file_date[$i],
								"size" => $file_size[$i]
								);
			array_push($json_infos["MP3_file"], $json_ligne );
		}		
	
		$tmp = json_encode($json_infos);
		echo $tmp;
	
}
	
	//echo "<br>";
	//print_r($tab);
	
function human_filesize($size, $precision = 2) 
{
    $units = array(' Octets',' KO','MO','GO','TO','POB','EO','ZO','YO');
    $step = 1024;
    $i = 0;
    while (($size / $step) > 0.9) {
        $size = $size / $step;
        $i++;
    }
    return round($size, $precision).$units[$i];
}
	
	
?>

</body>
</html>                                		