<?php

function objArraySearch($array, $index, $value) {
//echo "Hallo";die;
    foreach ($array as $arrayInf) {
//debug($value);
//debug(filter_var($arrayInf->{$index}, FILTER_SANITIZE_NUMBER_INT));
        if (filter_var($arrayInf->{$index}, FILTER_SANITIZE_NUMBER_INT) == $value) {
//debug($arrayInf);
            return $arrayInf;
        }
    }
    return null;
}

function obj2table($obj) {
    $a = get_object_vars($obj);
    echo "<table><tr>";
    foreach ($a as $key => $v) {
        echo "<td>$key</td>";
    }
    echo '</tr><tr>';
    foreach ($a as $v) {
        echo "<td>$v</td>";
    }
    echo "</tr></table>";
}

foreach ($data as $snr => $school) {

    echo '<h1>' . $snr . '</h1>';
    $vor = json_decode($school['vor']);
    $nach = json_decode($school['nach']);

    $i = 0;
    foreach ($vor as $teacher) {
        $pkz = filter_var($teacher->Name, FILTER_SANITIZE_NUMBER_INT);
//debug($int = filter_var($teacher->Name, FILTER_SANITIZE_NUMBER_INT));
        $index = objArraySearch($nach, 'Name', $pkz);
//debug($index);
//debug($i);
        if ($teacher != $index) {
            //debug($teacher);
            obj2table($teacher);
            //debug($index);
            if(!empty($index)) {
            obj2table($index);
            }
            else {
                echo '<h2>Lehrer ist weg</h2>';
            }
        }
        $i++;
    }
}



