How to change the order of this array?

I have a database table as follows:

couldn't be bothered with an image description, I guess

This returns all the column headers in the figure, but the most important are slug and parent (not sure about id_button).

The array is automatically ordered using id_button ASC, which really annoys me. But, in any case, this is not important, since I need to order it in a completely different way or reorder it after filling the array.

The array returns this in order of id_button:

$new_menu_buttons = array(
    0 => array(
            'id_button' => 1,
            'parent' => 'help',
            'position' => 'child_of',
            'slug' => 'testing',
    ),
    1 => array(
            'id_button' => 2,
            'parent' => 'packages',
            'position' => 'after',
            'slug' => 'sub_test_1',
    ),
    2 => array(
            'id_button' => 3,
            'parent' => 'google.com',
            'position' => 'after',
            'slug' => 'another_test',
    ),
    3 => array(
            'id_button' => 4,
            'parent' => 'testing'
            'position' => 'child_of',
            'slug' => 'google.com',
    )
);

I need to order it so that if a is slugfound in any parent, then it is necessary that the one slugin parentis loaded before the one that was defined in the parent.

, . , , testing - slug, , slug (google.com). , slug, , , , slug , .

, 3 :

$new_menu_buttons = array(
    0 => array(
            'id_button' => 1,
            'parent' => 'help',
            'position' => 'child_of',
            'slug' => 'testing',
    ),
    1 => array(
            'id_button' => 2,
            'parent' => 'packages',
            'position' => 'after',
            'slug' => 'sub_test_1',
    ),
    2 => array(
            'id_button' => 4,
            'parent' => 'testing',
            'position' => 'child_of',
            'slug' => 'google.com',
    ),
    3 => array(
            'id_button' => 3,
            'parent' => 'google.com'
            'position' => 'after',
            'slug' => 'another_test',
    )
);

...

$new_menu_buttons = array(
    0 => array(
            'id_button' => 1,
            'parent' => 'help',
            'position' => 'child_of',
            'slug' => 'testing',
    ),
    1 => array(
            'id_button' => 4,
            'parent' => 'testing',
            'position' => 'child_of',
            'slug' => 'google.com',
    ),
    2 => array(
            'id_button' => 2,
            'parent' => 'packages',
            'position' => 'after',
            'slug' => 'sub_test_1',
    ),
    3 => array(
            'id_button' => 3,
            'parent' => 'google.com'
            'position' => 'after',
            'slug' => 'another_test',
    )
);

...

$new_menu_buttons = array(
    0 => array(
            'id_button' => 1,
            'parent' => 'help',
            'position' => 'child_of',
            'slug' => 'testing',
    ),
    1 => array(
            'id_button' => 4,
            'parent' => 'testing',
            'position' => 'child_of',
            'slug' => 'google.com',
    ),
    2 => array(
            'id_button' => 3,
            'parent' => 'google.com'
            'position' => 'after',
            'slug' => 'another_test',
    ),
    3 => array(
            'id_button' => 2,
            'parent' => 'packages',
            'position' => 'after',
            'slug' => 'sub_test_1',
    )
);

3 , slug, parent, parent, slug, sub_test_1 parent , .

? , - , slug , - ...

, slug parent , parent, slug. , , .

+5
4

, . . , , , , , - ?

, !

$temp_buttons = array();
foreach($new_menu_buttons as $buttons)
    $temp_buttons[$buttons['parent']] = $buttons['slug'];


dp_sortArray($new_menu_buttons, $temp_buttons, 'slug');

// The $new_menu_buttons array is now sorted correctly!  Let check it...
var_dump($new_menu_buttons);

function dp_sortArray(&$new_menu_buttons, $sortArray, $sort)
{
    $new_array = array();
    $temp = array();
    foreach ($new_menu_buttons as $key => $menuitem)
    {
        if (isset($sortArray[$menuitem[$sort]]))
        {
            $new_array[] = $menuitem;

            $temp[$menuitem['parent']] = $menuitem['slug'];
            unset($new_menu_buttons[$key]);
        }   
    }

    $ordered = array();

    if (!empty($new_array))
    {
        foreach ($new_array as $key => $menuitem)
        {
            if (isset($temp[$menuitem[$sort]]))
            {
                $ordered[] = $menuitem;
                unset($new_array[$key]);
            }
        }
    }
    else
    {
        $new_menu_buttons = $new_menu_buttons;
        return;
    }

    $new_menu_buttons = array_merge($ordered, $new_array, $new_menu_buttons);
}

, , , , , -. ?

-1

Niko, , , , . SQL, ORDER BY. , MySQL 5.0: http://dev.mysql.com/doc/refman/5.0/en/sorting-rows.html

, PHP. , - , id ( ) , - fieldname → value ( ).

* ` - . - , , . , , ( ). , .

. , ( ), PHP , usort Docs.

-, , , , ( slug , ). , , .

/:

class menuButtons
{
    /**
     * @var array
     */
    private $buttons;

    public function __construct(array $buttons)
    {
        $this->buttons = $buttons;
    }


    public function sortChildsFirst()
    {
        $buttons = $this->buttons;
        usort($buttons, array($this, 'sortCallback'));
        return $buttons;
    }

    private function sortCallback($a, $b)
    {
        // an element is more than any other if it parent
        // value is any other slugs value

        if ($this->slugExists($a['parent']))
            return 1;

        return -1;
    }

    private function slugExists($slug)
    {
        foreach($this->buttons as $button)
        {
            if ($button['slug'] === $slug)
                return true;
        }
        return false;
    }
}

$buttons = new menuButtons($new_menu_buttons);

$order = $buttons->sortChildsFirst();

. , . , , , , . , .

, , . , . , usort, . , sortChildsFirst.

+3

, , , - . :

$tree = array();

foreach($array as $e) {
    $p = $e['parent'];
    $s = $e['slug'];
    if(!isset($tree[$p]))
        $tree[$p] = new stdclass;
    if(!isset($tree[$s]))
        $tree[$s] = new stdclass;
    $tree[$s]->data = $e;
    $tree[$p]->sub[] = $tree[$s];
}

data sub= . "" node, :

$out = array();

foreach($tree as $node)
    if(!isset($tree[$node->data['parent']]))
        add($out, $node);

add() -

function add(&$out, $node) {
    if(isset($node->data))
        $out[] = $node->data;
    if(isset($node->sub))
        foreach($node->sub as $n)
            add($out, $n);
}

, .

+1