Foreach loop not finding some values in array - weird problem?

Hi All

I have a very simple array which produces the following output when print_r() applied:

Array
(
[path] => Array
(
[0] => …//find_orphans/pics/hder.png
[1] => …//find_orphans/pics/Caure.PNG
[2] => …//find_orphans/pics/Caure2.PNG
[3] => …//ccount/reset.gif
[4] => …//ccount/delete.gif
[5] => …//ccount/edit.gif
[6] => …//ccount/line.gif
[7] => …//web_images/screen1.png
[8] => …//web_images/screen2.png
[9] => …//web_images/button.jpg
[10] => …//web_images/screen3.png
[11] => …//web_images/spacer.gif
)

[file] => Array
    (
        [0] => hder.png
        [1] => Caure.PNG
        [2] => Caure2.PNG
        [3] => reset.gif
        [4] => delete.gif
        [5] => edit.gif
        [6] => line.gif
        [7] => screen1.png
        [8] => screen2.png
        [9] => button.jpg
        [10] => screen3.png
        [11] => spacer.gif
    )

)

The foreach loop is as follows, again, pretty simple:

$count = 1;
$not_found = "";
$i = 0;
foreach($files[path] as $value){
        $is_linked = check_for_link($value,$files[file][$i]);
        echo $count.') '.$is_linked.'<br>';
        $not_found .= $value.',';
        $count++;
        $i++;
}

The check_for_link() function takes the file name ($files[file][$i]) and checks to see if that string exists in a pre-existing text based file.

function check_for_link($file_path,$file_name){

    $path = 'site_code.txt';
    $contents = file_get_contents($path);  
    $file_path = str_replace('..//','../',$file_path);
    if(!strpos($contents,$file_name)){
      return $file_path;
    }
}

The problem is when I try to echo the end result, it prints as follows:

1) …/find_orphans/pics/hder.png
2) …/find_orphans/pics/Caure.PNG
3) …/find_orphans/pics/Caure2.PNG
4)
5)
6)
7)
8) …/web_images/screen1.png
9) …/web_images/screen2.png
10) …/web_images/button.jpg
11) …/web_images/screen3.png
12) …/web_images/spacer.gif

Maybe I’m missing something painfully obvious, but why are 4,5,6 & 7 blank, has anyone come across this problem before? If so, I would be grateful to hear from you.

if I had to guess, the following values are not found in site_code.txt

[3] => reset.gif
[4] => delete.gif
[5] => edit.gif
[6] => line.gif

Secondly, you should really consider refactoring this MUCH further. For each item in your array, you are opening and closing the site_code.txt file. You would be better off, opening that file once, read it, store it in a variable, and reference that variable for the entire array.

Lastly, using $value,$files[file][$i] is unsafe in a foreach (the $i incrementer is not a guarantee to be associated to the correct value in the foreach array). A foreach is not guaranteed to read the array in a linear order (unlike for which is guaranteed to do that). That may also be part of your problem.

Hi cpradio - thanks for reply and advice. I think you’ve steered me in the right direction.

My script is only in its infancy. I think I’ll re-write this code and start from square 1.

Thanks again.