Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php-foreach函数并动态更新xml文件_Php_Xml_Foreach - Fatal编程技术网

php-foreach函数并动态更新xml文件

php-foreach函数并动态更新xml文件,php,xml,foreach,Php,Xml,Foreach,我正在使用此代码动态更新xml文件 我的问题是,我对如何应用操作顺序感到困惑: <?php $files = scandir('./resources'); sort($files); foreach($files as $file){ if(is_dir("./resources/$file") and $file != "." && $file != ".."){ myFun($file); } } $simplexml = simp

我正在使用此代码动态更新xml文件

我的问题是,我对如何应用操作顺序感到困惑:

<?php

$files = scandir('./resources');
sort($files);
foreach($files as $file){
    if(is_dir("./resources/$file") and $file != "." && $file != ".."){
        myFun($file);
    }
}

$simplexml = simplexml_load_file('map.xml');

function myFun($scannedTitle){

    $tags = get_meta_tags('./resources/'.$scannedTitle.'/index.html');
    $scannedDescription = $tags['description'];


    $items = $simplexml->xpath('channel/item[@id="'.$scannedTitle.'"]');
    $items = $items[0];

    if($items->title[0] == $scannedTitle){

    }else{
        $items->title[0] = $scannedTitle;
    }

    if($items->description[0] == $scannedDescription){

    }else{
        $items->description[0] = $scannedDescription;
    }

}

$simplexml->saveXML("map.xml");

?>
xpath('channel/item[@id=“.”.$scannedTitle.'']”);
$items=$items[0];
如果($items->title[0]=$scannedTitle){
}否则{
$items->title[0]=$scannedTitle;
}
如果($items->description[0]=$scannedDescription){
}否则{
$items->description[0]=$scannedDescription;
}
}
$simplexml->saveXML(“map.xml”);
?>

代码正在工作,但现在我更改了函数的顺序,它不再工作了。
我不明白我必须按什么顺序执行这些动作

这里是我的“map.xml”文件:


红色
http://website.com/resources/red/
描述红色
绿色
http://website.com/resources/green/
绿色页面的说明。
蓝色
http://website.com/resources/blue/
蓝色页面的描述。

您有一个变量范围问题,
myFun()
将如何处理函数外部创建的
$simplexml

我认为,通过在函数中传递对
$simplexml
的引用,它将以这种方式工作

 <?php
 $simplexml = simplexml_load_file('map.xml');

 $files = scandir('./resources');
 sort($files);
 foreach($files as $file){
     if(is_dir("./resources/$file") and $file != "." && $file != ".."){
         myFun($file, $simplexml);
     }
 }


 function myFun($scannedTitle, &$simplexml){

     $tags = get_meta_tags('./resources/'.$scannedTitle.'/index.html');
     $scannedDescription = $tags['description'];


     $items = $simplexml->xpath('channel/item[@id="'.$scannedTitle.'"]');
     $items = $items[0];

     if($items->title[0] == $scannedTitle){

     }else{
         $items->title[0] = $scannedTitle;
     }

     if($items->description[0] == $scannedDescription){

     }else{
         $items->description[0] = $scannedDescription;
     }

 }

 $simplexml->saveXML("map.xml");

 ?> 
xpath('channel/item[@id=“.”.$scannedTitle.'']”);
$items=$items[0];
如果($items->title[0]=$scannedTitle){
}否则{
$items->title[0]=$scannedTitle;
}
如果($items->description[0]=$scannedDescription){
}否则{
$items->description[0]=$scannedDescription;
}
}
$simplexml->saveXML(“map.xml”);
?> 

xml文件是否已创建?因为我没有看到你测试它是否存在。
 <?php
 $simplexml = simplexml_load_file('map.xml');

 $files = scandir('./resources');
 sort($files);
 foreach($files as $file){
     if(is_dir("./resources/$file") and $file != "." && $file != ".."){
         myFun($file, $simplexml);
     }
 }


 function myFun($scannedTitle, &$simplexml){

     $tags = get_meta_tags('./resources/'.$scannedTitle.'/index.html');
     $scannedDescription = $tags['description'];


     $items = $simplexml->xpath('channel/item[@id="'.$scannedTitle.'"]');
     $items = $items[0];

     if($items->title[0] == $scannedTitle){

     }else{
         $items->title[0] = $scannedTitle;
     }

     if($items->description[0] == $scannedDescription){

     }else{
         $items->description[0] = $scannedDescription;
     }

 }

 $simplexml->saveXML("map.xml");

 ?>