I have this foreach for retrieving subdirectories and files and then storing them in an array, I'm using RecursiveDirectoryIterator but I'm open to use any other that can solve my issue:
$it = new RecursiveDirectoryIterator($path);
foreach(new RecursiveIteratorIterator($it) as $file){
if ($file->getExtension() == 'jpg'){
$folder = ltrim($file->getPath(), $path."/");
$images[] = array("cod" => $folder, "images" => $file->getFilename());
}
}
And the output is:
array(294) {
[0]=>
array(2) {
["cod"]=>
string(5) "8322C"
["images"]=>
string(36) "8322c-pastillas-del-sedona0-12-2.jpg"
}
[1]=>
array(2) {
["cod"]=>
string(5) "8322C"
["images"]=>
string(36) "8322c-pastillas-del-sedona0-12-1.jpg"
}
[2]=>
array(2) {
["cod"]=>
string(5) "7877C"
["images"]=>
string(29) "7877c-pastillas-4runner-2.jpg"
}
[3]=>
array(2) {
["cod"]=>
string(5) "7877C"
["images"]=>
string(29) "7877c-pastillas-4runner-1.jpg"
}
[4]=>
array(2) {
["cod"]=>
string(9) "D889-7767"
["images"]=>
string(30) "pastilla-spectra-rio-bex-1.jpg"
}
...
So what I want to achieve is to not repeat the variable cod (which in this case is the directory), so it looks like:
array(294) {
[0]=>
array(2) {
["cod"]=>
string(5) "8322C"
["images"]=>
string(74) "8322c-pastillas-del-sedona0-12-1.jpg, 8322c-pastillas-del-sedona0-12-2.jpg"
}
[1]=>
array(2) {
["cod"]=>
string(5) "7877C"
["images"]=>
string(60) "7877c-pastillas-4runner-1.jpg, 7877c-pastillas-4runner-2.jpg"
}
[3]=>
array(2) {
["cod"]=>
string(9) "D889-7767"
["images"]=>
string(30) "pastilla-spectra-rio-bex-1.jpg"
}
...
Can you help me please? Is that possible?