It seems like you're trying to link database items via a many-to-many relationship.
To map this in a relational SQL database, you would have one table containing your nodes/items, where you can describe their "name" and any other attributes they have:
item_id | name
----------------
0 | test
1 | abcd
2 | node_2
3 | item_3
4 | item_4
And then you would have another table mapping elements (0->2
, 0->4
) together, which describes a connection between two nodes in the graph, as the following connection
table:
connection_id | item_id_1 | item_id_2
--------------------------------------
0 | 0 | 2
1 | 0 | 4
Each connection has it's own id for easy editing, and it links to the items via their ids. You can specify as many connections between nodes as you like in this table. This maps a one-way, many-to-many relationship. You can use a similar table to map two different entities together, by just mapping the ids.
As an example, you can then run a query to get the names of two joined items like so:
SELECT
i1.name AS 'item_1_name'
i2.name AS 'item_2_name'
FROM
connection AS c
INNER JOIN items AS i1
ON i1.item_id = c.item_id_1
INNER JOIN items AS i2
ON i2.item_id = c.item_id_2
Which would return the following:
item_1_name | item_2_name
-------------------------
test | node_2
test | item_4
For more detailed info on setting up the tables, there is this stackoverflow question: How to make SQL many-to-many same-type relationship table although his answer about selecting them is vulnerable to SQL injection, so I'd be very careful with the PHP variables he mentions.