children() as $child) { $arrAllFile[] = $child; } return $arrAllFile; } /** * @param $obj1 * @param $obj2 * @return array */ function getDifference ($obj1, $obj2) { $arrDiff = array(); if (json_encode($obj1['id']) != json_encode($obj2['id'])) { if (isset($arrDiff['Catalog']) && !is_array($arrDiff['Catalog'])) { $arrDiff['Catalog'] = array(); } $arrDiff['Catalog']['f1-id'] = $obj1['id']; $arrDiff['Catalog']['f2-id'] = $obj2['id']; } //check version if (json_encode($obj1['version']) != json_encode($obj2['version'])) { if (isset($arrDiff['Catalog']) && !is_array($arrDiff['Catalog'])) { $arrDiff['Catalog'] = array(); } $arrDiff['Catalog']['f1-version'] = $obj1['version']; $arrDiff['Catalog']['f2-version'] = $obj2['version']; } $c = count($obj1->children()); // prüf parameter for ($j = 0; $j <= $c; $j++) { if (isset($arrDiff['Parameter']) && !is_array($arrDiff['Parameter'])) { $arrDiff['Parameter'] = array(); } if(json_encode($obj1->children()[$j]['name']) != json_encode($obj2->children()[$j]['name'])) { $arrDiff['Parameter']['f1-name-'.$j] = $obj1->children()[$j]['name']; $arrDiff['Parameter']['f2-name-'.$j] = $obj2->children()[$j]['name']; } if(json_encode($obj1->children()[$j]['value']) != json_encode($obj2->children()[$j]['value'])) { $arrDiff['Parameter']['f1-value-'.$j] = $obj1->children()[$j]['value']; $arrDiff['Parameter']['f2-value-'.$j] = $obj2->children()[$j]['value']; } } if (empty($arrDiff['Catalog']) && empty($arrDiff['Parameter'])) { return []; } return $arrDiff; } $arrFile1 =simplexml_load_file("default_sysdumpdev_150319.txt") or die("Error: Cannot create object"); $arrFile2 =simplexml_load_file("d100spuptl25e0_sysdumpdev.txt") or die("Error: Cannot create object"); $arrAllFile1 = addObjectElementsToArray ($arrFile1); $arrAllFile2 = addObjectElementsToArray ($arrFile2); $arrDifferences = array(); if (count($arrAllFile1) != count($arrAllFile2)) { $arrDifferences['differentSize'] = "Die Anzahl der Elemente in beide Dateien Stimmen nicht überein"; //exit } else if (count($arrAllFile1 != count($arrAllFile2))) { //loop and compare step step by step $arrDifferenceList = array(); for ($i = 0; $i < count($arrAllFile1); $i++) { if (json_encode($arrAllFile1[$i]) != json_encode($arrAllFile2[$i])) { //pruf jeder element des nicht gleiche obj element $diff = getDifference($arrAllFile1[$i], $arrAllFile2[$i]); if (!empty($diff)) { $arrDifferenceList[] = $diff; } } } echo "
";
    $arrDifferences['differences'] = $arrDifferenceList;
}


// Schreibe die unterschieden in eine result Datei
$content = '';
if (isset($arrDifferences['differentSize'])) {
    $content = $arrDifferences['differentSize'] . '\n';
}

if (isset($arrDifferences['differences'])) {
    print_r($arrDifferences);
    foreach ($arrDifferences['differences'] as $diff) {
        foreach($diff as $key => $arr) {
            if ($key == 'Catalog') {
                $content .= 'CATALOG:' . PHP_EOL;
                foreach ($arr as $key => $value) {
                    $content .= $key . ' = '.$value . PHP_EOL;
                }
            }

            if ($key == 'Parameter') {
                $content .= PHP_EOL . 'PARAMETER:' . PHP_EOL;
                foreach ($arr as $key => $value) {
                    $content .= $key . ' = '.$value . PHP_EOL;
                }
            }
        }
    }

}

if ($content == '') {
    $content = 'Es git keine Unterschied in Beide Dateien';
}
// erstellt die datei result.txt oder lösche die inhalte der bereit existierende
$file = fopen("result.txt","w+");
fwrite($file,$content ,strlen($content));
fclose($file);