Search code examples
neo4jneo4jphp

Use neo4jphp to get an array with all the nodes related to a master node


I use Neo4jPHP to write a snippet which will give me an array of all the nodes which are connected with a "master" node (A). The node connect like this:

  A -> B
  B -> C
  C -> D
  C -> E
  B -> F

This is the code I wrote using neo4jPHP:

$client = new Everyman\Neo4j\Client();
$querystring="MATCH path=(n {gid:'58731'})-[:contains*]-(z) RETURN ([x in nodes(path) | x.id]) as names";
$query=new Everyman\Neo4j\Cypher\Query($client,$querystring); 
$result=$query->getResultSet();

foreach($result as $resultItem){
    $resultArray[] = $resultItem['n'];
}   
print_r($resultArray); // prints the array

The problem is that the $resultArray stores the nodes which relate to the master node repeatedly (like explained here: Strange behavior in neo4j when I try to get all the nodes which relate with a master node )

My question is: Is there a way using neo4jPhp to get back an array which will contain all the nodes which are related to the "master" node only once? Something like this:

 $distinctNodes = [B,C,D,E,F]

Thanks D.


Solution

  • This query should directly return the array:

    MATCH (n {gid:'58731'})-[:contains*1..]->(z) RETURN COLLECT(DISTINCT z.id) as names