Array is bigger then my XML tags? (PHP)

Question

I want to add a child with a specified value which I get from my array. But my problem is that my array is bigger then my amount of XML products... This is why I get this error message:

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

To check that I made two loops which say me that I have 1588 names and 1588 products, both loops say that. Thats logical!

$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;
}

But my array in which I store the specified value for each product is 1589 (0 - 1588) values big... And I don't know.

Can anyone help me and tell me the error?

$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');
}

Error is in line 63:

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

Greetings and Thank You!


Show source
| dom   | xml   | php   | arrays   | domdocument   2017-01-06 13:01 0 Answers

Answers ( 0 )

◀ Go back