2

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?

Luis
  • 31
  • 5

1 Answers1

0

You can first check if the element is repeated, and add it to array if it's not, else add the new one in existing entry:

$index = findArrayInArray($images, "cod", $folder);
if($index >= 0)
    $images[$index]["images"] .= ", ".$file->getFilename());
else
    $images[] = array("cod" => $folder, "images" => $file->getFilename());

instead of

$images[] = array("cod" => $folder, "images" => $file->getFilename());

Using a simple function which searches for an array inside array (using a key and value):

function findObjectInArray($arr, $key, $val){
    for($i = 0; $i < count($arr); $i++)
        if (isset($arr[$i][$key]) and $arr[$i][$key] == $val)
            return $arr[$i];
        return -1;
}
  • Hello, thank you for your reply, I fixed some typos in your code (an extra ")" and the name of the function), but is throwing this error: _Illegal offset type_ in this line: `$images[$index][$images] .= ", ".$file->getFilename();` – Luis Apr 04 '20 at 20:13
  • Oh, I'm sorry that was supposed to be string. I edited that line. Please try to edit my answer with those typos you fixed. Thanks in advance. – Kamyar Mirzavaziri Apr 04 '20 at 23:38
  • Maybe you didn't remove the wrong `$`, this should work. – Kamyar Mirzavaziri Apr 05 '20 at 05:12
  • Yes I did like this: `$images[$index]["images"] .= ", ".$file->getFilename();` – Luis Apr 06 '20 at 15:21
  • Thank you for your effort anyway! I found the solution here: https://stackoverflow.com/questions/51507724/combining-duplicate-keys-in-a-multidimensional-array-in-php – Luis Apr 06 '20 at 21:30