For testing purpose, i am Looking to convert an array into a valid Json format. To do this, I get a list of files from a directory. This gives me as result:
{"data":["file1.txt","file2.txt","file3.txt"]}
Here's my code:
public function getDirectories() {
$dir = $this->getConfig(); // My Directory
$array = array();
$handle = opendir($dir);
while (FALSE !== ($entry = readdir($handle))) {
if ($entry == '.' || $entry == '..')
continue;
$array[] = $entry;
}
return $array;
}
And My class for processing:
class ArrayValue implements JsonSerializable {
public function __construct(array $array) {
$this->array = $array;
}
public function jsonSerialize() {
return array('data' => $this->array);
}
}
I think I have a here a json result format certainly, but this appears to be invalid, since I have an array in the json. So How do I proceed to get the following format like this?
{"data":{"file1.txt","file2.txt","file3.txt"}}