Skip to content

Instantly share code, notes, and snippets.



Created May 20, 2014
What would you like to do?
EasyRDF graph-merge example
require '../vendor/autoload.php';
$graph1 = new EasyRdf_Graph();
$graph1->addResource('', 'dc:title', 'Hello, world!');
$graph1->addResource('', 'dc:description', 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.');
$graph2 = new EasyRdf_Graph();
$graph2->addResource('', 'dc:description', 'Some boring description');
$graph2->addResource('', 'dc:title', 'Hello, second world!');
var_dump(mergeGraphs($graph1, $graph2)->toRdfPhp());
* @param EasyRdf_Graph $graph1
* @param EasyRdf_Graph $graph2
* @return EasyRdf_Graph
function mergeGraphs(EasyRdf_Graph $graph1, EasyRdf_Graph $graph2)
$data1 = $graph1->toRdfPhp();
$data2 = $graph2->toRdfPhp();
$merged = array_merge_recursive($data1, $data2);
unset($data1, $data2);
return new EasyRdf_Graph('urn:easyrdf:merged', $merged, 'php');

This comment has been minimized.

Copy link

@hackerceo hackerceo commented Jun 8, 2015

I don't this this would correctly handle merging two graphs with blank nodes. I am assuming that we are NOT attempting to identify and collapse identical bnode subgraphs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment