Even easier: 

    $p = array_map(function() {
        if ($element) {
            return $element;
        }
    }, $path_elements);