Skip to content
Advertisement

Loop through array, compare values with object values and define a new array, based on those values

I have images saved in db and on server. I have to get images from db and compare them with those on server. If match, then show images (make new array with matched image names and fetch them in browser)

Example:

$db_image_array

 Array
 (
 [0] => Array
    (
        [id] => 14
        [eid] => 27
        [stype_id] => 1  // folder1
        [ihash] => oOYvcSEGeUjDpL2a.jpg
    )

[1] => Array
    (
        [id] => 19
        [eid] => 27
        [stype_id] => 3  // folder3 
        [ihash] => XYmpP49Z5ye2OTL8.jpg
    )

[2] => Array
    (
        [id] => 20
        [eid] => 27
        [stype_id] => 1 // folder1
        [ihash] => uqF6whY6F6zNmdC7.jpg
    )

[3] => Array
    (
        [id] => 21
        [eid] => 27
        [stype_id] => 1 // folder1
        [ihash] => KXuYIidPpHrkZc96.jpg
    )

)

$serverFolderArray

 Array
 (
   [folder1] => Array
    (
        [0] => SplFileInfo Object
            (
                [pathName:SplFileInfo:private] => /var/www/vhosts/img/1629973f75566aed/folder1/uqF6whY6F6zNmdC7.jpg
                [fileName:SplFileInfo:private] => uqF6whY6F6zNmdC7.jpg
            )
        [1] => SplFileInfo Object
            (
                [pathName:SplFileInfo:private] => /var/www/vhosts/img/1629973f75566aed/folder1/oOYvcSEGeUjDpL2a.jpg
                [fileName:SplFileInfo:private] => oOYvcSEGeUjDpL2a.jpg
            )
        [2] => SplFileInfo Object
            (
                [pathName:SplFileInfo:private] => /var/www/vhosts/img/1629973f75566aed/folder1/KXuYIidPpHrkZc96.jpg
                [fileName:SplFileInfo:private] => KXuYIidPpHrkZc96.jpg
            )
    )

[folder2] => Array
    (
    )
[folder3] => Array
    (
        [0] => SplFileInfo Object
            (
                [pathName:SplFileInfo:private] => /var/www/vhosts/img/1629973f75566aed/folder3/XYmpP49Z5ye2OTL8.jpg
                [fileName:SplFileInfo:private] => XYmpP49Z5ye2OTL8.jpg
            )
    )

[folder4] => Array
    (
    )
 )

My first try was this code:

 if ($db_image_array) {
     foreach ($serverFolderArray as $arrayName => $serverImageArray) {
       if ( ! empty($serverImageArray)) {
           for ($y = 0; $y < count($db_image_array); $y++) {
               for ($x = 0; $x < count($serverImageArray); $x++) {
                   if ($serverImageArray[$x]->getFilename() === $db_image_array[$y]['ihash']) {
                       $arr[$x] = $db_image_array[$y]['ihash'];
                       $chunked_array[$arrayName] = array_chunk($arr, 4);
                   }
               }
           }
       }
   }

}

there was also a second try with foreach loop:

    if ( ! empty($serverImageArray)) {
        for ($x = 0; $x < count($serverImageArray); $x++) {
            foreach ($db_image_array as $key => $val) {
                if ($serverImageArray[$x]->getFilename() === $val['ihash']) {
                    $arr[$x] = $val['ihash'];
                    $chunked_array[$arrayName] = array_chunk($arr, 4);
                    break;
                }
            }
        }
    }

both ways give me wrond image sort chunked_array:

Array
(
  [folder1] => Array
    (
        [0] => Array
            (
                [0] => oOYvcSEGeUjDpL2a.jpg
                [1] => uqF6whY6F6zNmdC7.jpg
                [2] => KXuYIidPpHrkZc96.jpg
            )
    )
  [folder3] => Array
    (
        [0] => Array
            (
                [0] => oOYvcSEGeUjDpL2a.jpg // this one has not to be here
                [1] => XYmpP49Z5ye2OTL8.jpg 
                [2] => KXuYIidPpHrkZc96.jpg // and this one also doesn't have to be in this array
            )
      )
)

How do you see this situation? Where is a logic mistake?

Advertisement

Answer

So, was struggling with this bug for hours, but 30 min after creating this topic found a logic bug, I have to clear $arr each time new array name comes in:

$arr=[];  //  <----  !!!
if ( ! empty($serverImageArray)) {
    for ($x = 0; $x < count($serverImageArray); $x++) {
        foreach ($db_image_array as $key => $val) {
            if ($serverImageArray[$x]->getFilename() === $val['ihash']) {
                $arr[$x] = $val['ihash'];
                $chunked_array[$arrayName] = array_chunk($arr, 4);           
            }
        }
    }
}
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement