I am attempting to find all rows of an array that contain a specific sub-string.
I have an array containing all my product numbers or SKU's.
I have another array containing paths to images which are found within a directory and its many sub-directories.
(This array of image paths is populated using a RecursiveIterator that goes through the directory and adds all the file paths to this array)
Each image name contains the SKU within it, so sku# 123 may have the following images:
123.jpg
123_1.jpg
123_2.jpg
etc. etc.
I want to output all images that are associated with a particular sku. Here is the code I have started. For some reason I am only getting the desired result for the last sku.
$dir = "./images"; // directory with images
$skus = file("./source.csv"); // source file with all skus
$all_images = array(); // array to hold all image paths found in folder
// recursively search through directory save all file paths to array
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir)) as $f) {
$all_images[] = $f;
}
// loop through each sku and find its image paths
for($i=0; $i < count($skus); $i++){
$values = preg_grep("/$skus[$i]/", $all_images);
echo "----sku: $skus[$i] -----<br /><br />";
foreach($values as $val)
echo "Values: $val<br />";
}
My result page looks something like this:
----sku: TR450 -----
----sku: TR451 -----
----sku: TR452 -----
----sku: TR453 -----
----sku: TR454 -----
----sku: TR455 -----
----sku: TR456 -----
Values: ./images\brand\make\TR456 - abacus\TR457.jpg
Values: ./images\brand\make\TR457 - abacus\TR457_Diagram.jpg
Values: ./images\brand\make\TR458 - abacus\Thumbs.db
I'm not sure why this is only working for the last SKU??
Thank you.