数组比我的XML标签大? (PHP)

时间:2017-01-06 12:23:45

标签: php arrays xml dom domdocument

我想添加一个具有指定值的子项,这是从数组中获取的。但我的问题是我的数组比我的XML产品数量还要大...... 这就是我收到此错误消息的原因:

PHP Notice:  Undefined offset: 1589 in C:\Users\Jan\PhpstormProjects\censored\test.php on line 63
PHP Fatal error:  Uncaught Error: Call to a member function appendChild() on null in C:\Users\Jan\PhpstormProjects\censored\test.php:64

要检查我做了两个循环,说我有1588个名字和1588个产品,两个循环都说。这是合乎逻辑的!

$markerProduct = $root->getElementsByTagName("product");
for($i = $markerProduct->length - 1; $i >= 0; $i--){
    $productCounter = $productCounter + 1;
}

$markerTitle = $root->getElementsByTagName("name");
for($i = 0; $i < $markerTitle->length; $i++){
    $nameCounter = $nameCounter + 1;
}

但是我存储每个产品的指定值的数组是1589(0 - 1588)值大...我不知道。

任何人都可以帮助我并告诉我错误吗?

$searches = ["Steam", "Uplay", "Xbox", "Nintendo", "PS3", "PS4", "PSN"];

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;

        $co = count($searches);
        $productFound = false;
                for($j = 0; $j < $co; $j++){
            if(stripos($title, $searches[$j]) !== false){
                if($j > 3){
                    array_push($plat, "PlayStation");
                }else{
                    array_push($plat, $searches[$j]);
                }
                $productFound = true;
            }elseif(stripos($url, $searches[$j]) !== false){
                if($j > 3){
                    array_push($plat, "PlayStation");
                }else{
                    array_push($plat, $searches[$j]);
                }
                $productFound = true;
            }
        }
        if($productFound == false){
            array_push($plat, "Nothing");
        }
    }

    print_r($plat);
    $c = count($plat);
    echo $c;

    for($i = $c - 1; $i >= 0; $i--){
        $node = $dom->createElement('plat', $plat[$c]);
        $dom->getElementsByTagName($productTag)->item($i)->appendChild($node);
    }


    $dom->saveXML();
    $dom->save('data/gamesplanet2.xml');
}

错误在第63行:

$node = $dom->createElement('plat', $plat[$c]);

问候,谢谢!

0 个答案:

没有答案