dqroktbn005028 2013-04-01 06:48
浏览 48
已采纳

为什么我没有在recursiveDirectoryItorator中访问值的范围?

I'm playing around with the PHP RecursiveDirectoryItorator, but I found that for some reason some of my variables are out of scope when they should be in scope... unless I'm really missing something. the two variables I'm trying to access are the $master_array, and the $current, both of witch do not have any values if i echo or print_r them in my else statement. here is my code:

$master_array = array(); 

$startpath = "some file path"; // i'm using a real file path in my code. 
$ritit = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($startpath), RecursiveIteratorIterator::CHILD_FIRST); 

foreach($ritit as $fileinfo) {
    echo "<pre>"; 
    $current = ''; 
    if(!$fileinfo->isFile()) {

        if(strip_replace($fileinfo->getFilename()) == strip_replace('some  - title')) {
            $master_array[$fileinfo->getFilename()]  = $fileinfo->getPathname(); 
            $current = $fileinfo->getFilename(); 
        } 

    } else {

        if(!empty($current) && in_array($current, split_file_path_into_array($fileinfo->getPathname()))) {
            echo $fileinfo->getFilename()."
"; 
            echo $fileinfo->getPathname()."
"; 
        } 
        echo $current; // i don't have access to this
        print_r($master_array); // and i don't have access to this.  

        }
    }   

any help with this would be greatly appreciated. I hope I'm just overlooking something simple, but I've been looking at this code, and messing with it for quite some time, and can't seem to get it to work.

  • 写回答

1条回答 默认 最新

  • dth42345 2013-04-01 06:51
    关注

    $current should be array not string

    $current = array();
    

    And $current will always be empty because you are calling it inside else and assigning value inside if's if.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化