The class:
class deps{
var $items;
function add($item, $deps = array()){
$this->items[$item] = $deps;
}
}
How can I generate an array with $items ordered by taking into account dependencies ($deps) ?
For example:
$deps = new deps;
$deps->add('item2', array('item1')); // <- depends on item1
$deps->add('item1', array()); // <- no dependency
$deps->add('item3', array('item1', 'item5')); // <- depends on item1 and item5
$deps->add('A', array('item3')); // <- on item3
$deps->add('C', array('item2', 'item1')); // ......
The ordered array would be:
item1
item2
C
And a second array, with items that needed one or more dependencies that didn't exist:
item3
A
With the data you provided, this did the trick for ordering the dependencies.
The items left in $temporary are the unresolved dependencies and for your data they were returned as expected but I assume this is a coincidence.
I am not sure as to how to order unresolved dependencies, probably by how many dependencies where unresolved for these items.
Something like:
http://codepad.org/fSwJjyz5
Bit messy, but returns the correct load order (1, 2, C) and unresolved.
Called:
Produces: