SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot 2ndmouse's Avatar
    Join Date
    Jan 2007
    Location
    West London
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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:

    PHP Code:
    $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.

    PHP Code:
    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.
    Detect file changes remotely. SimpleSiteAudit is an early
    warning anti-hacker system which sends an alert on detection.

    PHP Find Orphan Files - Finds all the unreferenced files on your site.

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,067
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    if I had to guess, the following values are not found in site_code.txt
    Code:
    [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.
    Be sure to congratulate Patche on earning July's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  3. #3
    SitePoint Zealot 2ndmouse's Avatar
    Join Date
    Jan 2007
    Location
    West London
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.
    Detect file changes remotely. SimpleSiteAudit is an early
    warning anti-hacker system which sends an alert on detection.

    PHP Find Orphan Files - Finds all the unreferenced files on your site.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •