Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/277.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.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
XML:为每个产品添加子项-不工作(PHP)_Php_Xml - Fatal编程技术网

XML:为每个产品添加子项-不工作(PHP)

XML:为每个产品添加子项-不工作(PHP),php,xml,Php,Xml,我想通过从标题或url获取平台来为每个产品添加平台。但问题是我保存平台表单的数组没有产品列表那么大。但那不可能! 因为在每种情况下,我都会将一个值放入数组中。。。所以它应该同样大 但它不是。。。此外,代码没有将子项添加到XML中。但我不知道为什么。。。我怎样才能解决这个问题 代码: 函数包含($searches、$titleTag、$urlTag、$productTag、$path){ $dom=新的DOMDocument('1.0','utf-8'); $dom->preserveWhiteS

我想通过从标题或url获取平台来为每个产品添加平台。但问题是我保存平台表单的数组没有产品列表那么大。但那不可能! 因为在每种情况下,我都会将一个值放入数组中。。。所以它应该同样大

但它不是。。。此外,代码没有将子项添加到XML中。但我不知道为什么。。。我怎样才能解决这个问题

代码:

函数包含($searches、$titleTag、$urlTag、$productTag、$path){
$dom=新的DOMDocument('1.0','utf-8');
$dom->preserveWhiteSpace=false;
$dom->formatOutput=true;
$dom->load($path);
$root=$dom->documentElement;
$markerTitle=$root->getElementsByTagName($titleTag);
$markerURL=$root->getElementsByTagName($urlTag);
$plat=array();
对于($i=$markerTitle->length-1;$i>=0;$i--){
$title=$markertite->item($i)->textContent;
$url=$markerURL->item($i)->textContent;
$size=计数($search);
对于($j=0;$j<$size;$j++){
if(stripos($title,$searches[$j])){
如果($j>4){
阵列推送($plat,“PlayStation”);
}elseif($j<5){
数组推送($plat,$searches[$j]);
}否则{
阵列推送($plat,“Nothing”);
}
}elseif(stripos($url,$searches[$j])){
如果($j>4){
阵列推送($plat,“PlayStation”);
}elseif($j<5){
数组推送($plat,$searches[$j]);
}否则{
阵列推送($plat,“Nothing”);
}
}
}
}
阵列_反向($plat);
印刷费($plat);
$count=0;
foreach($root->getElementsByTagName($productTag)作为$product){
$newElement=$dom->createElement('plat',$plat[$count]);
$product->appendChild($newElement);
$count=$count+1;
}
}
XML:


最终幻想七蒸汽
最终幻想VII gibt es jetzt für Den PC,mit brandneuen在线功能!
11.69
12.99
https://de.gamesplanet.com/game/final-fantasy-vii-download--1001-1
艾多斯广场酒店
RPG(Rollenspiel)
https://de.gamesplanet.com/acache/10/01/1/de/packshot-770bf6d03800b87dc0f9509f21e8d423.jpg

您好

看起来您使用的是
stripos()
,比较松散。索引的零值(在第一个位置找到的字符串)将不正确地为false。使用
if(stripos($title,$searches[$j])!==false)
进行与false的严格比较。但是我不确定这是您的问题的原因还是您唯一的问题。另外,您没有外部
if/elseif
结构的
else{}
大小写,因此不会将不匹配项添加到数组中。您的意思是:您也没有else{}外部if/elseif结构的大小写,这样就不会向数组中添加不匹配项。?从
if开始(stripos($title,$searches[$j])){
您有
if/elseif
,但您没有最终的
else
来捕获既不匹配
$title
也不匹配
$url
的字符串。可能是因为您没有严格地与
!==false
进行比较,所以有些节点失败了,不匹配
$title
$url
,从而导致错误在比预期更少的数组项中。
function isContaining($searches, $titleTag, $urlTag, $productTag, $path){

$dom = new DOMDocument('1.0', 'utf-8');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->load($path);
$root = $dom->documentElement;

$markerTitle = $root->getElementsByTagName($titleTag);
$markerURL = $root->getElementsByTagName($urlTag);

$plat = array();

for($i = $markerTitle->length - 1; $i >= 0 ; $i--){

    $title = $markerTitle->item($i)->textContent;
    $url = $markerURL->item($i)->textContent;

    $size = count($searches);

    for($j = 0; $j < $size; $j++){
        if(stripos($title, $searches[$j])){
            if($j > 4){
                array_push($plat, "PlayStation");
            }elseif($j < 5){
                array_push($plat, $searches[$j]);
            }else{
                array_push($plat, "Nothing");
            }
        }elseif(stripos($url, $searches[$j])){
            if($j > 4){
                array_push($plat, "PlayStation");
            }elseif($j < 5){
                array_push($plat, $searches[$j]);
            }else{
                array_push($plat, "Nothing");
            }
        }
    }
}

array_reverse($plat);
print_r($plat);
$count = 0;

foreach($root->getElementsByTagName($productTag) as $product){
    $newElement = $dom->createElement('plat', $plat[$count]);
    $product->appendChild($newElement);
    $count = $count + 1;
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<products>
  <product>
    <name>Final Fantasy VII Steam</name>
    <desc>Den Rollenspiel-Klassiker FINAL FANTASY VII gibt es jetzt für den PC, mit brandneuen Online-Features!</desc>
    <price>11.69</price>
    <price_base>12.99</price_base>
    <link>https://de.gamesplanet.com/game/final-fantasy-vii-download--1001-1</link>
    <publisher>Eidos - Square Enix</publisher>
    <category>RPG (Rollenspiel)</category>
    <ean/>
    <packshot>https://de.gamesplanet.com/acache/10/01/1/de/packshot-770bf6d03800b87dc0f9509f21e8d423.jpg</packshot>
  </product>