Array sorting help...please :)

In php…How can I sort the following example by “attachment_id” under each individual array [image][videoembed][etc] So [image] would be ordered 691,692,699 and then [videembed] would be similarly ordered if there were more than 1…etc, etc.

I really hope that makes sense.

Thank you very much all.


(
[image] => Array
    (
        [0] => stdClass Object
            (
                [title] => 77007_433592946695465_6701985_n.jpg
                [attachment_id] => 699
            )

        [1] => stdClass Object
            (
                [title] => 253163_10151127010009522_736212653_n.jpg
                [attachment_id] => 692
            )

        [2] => stdClass Object
            (
                [title] => 263893_10150217397944522_4550001_n.jpg
                [attachment_id] => 691
            )

    )

[videoembed] => Array
    (
        [0] => stdClass Object
            (
                [title] => YouTube
                [attachment_id] => 692
            )

    )

[] => Array
    (
        [0] => stdClass Object
            (
                [title] => yada.jpg
                [attachment_id] => 688
            )

    )

)

So, this example would result as such:


(
[image] => Array
    (
        [2] => stdClass Object
            (
                [title] => 263893_10150217397944522_4550001_n.jpg
                [attachment_id] => 691
            )
        [1] => stdClass Object
            (
                [title] => 253163_10151127010009522_736212653_n.jpg
                [attachment_id] => 692
            )
        [0] => stdClass Object
            (
                [title] => 77007_433592946695465_6701985_n.jpg
                [attachment_id] => 699
            )

    )
[videoembed] => Array
    (
        [0] => stdClass Object
            (
                [title] => YouTube
                [attachment_id] => 692
            )

    )

[] => Array
    (
        [0] => stdClass Object
            (
                [title] => yada.jpg
                [attachment_id] => 688
            )

    )

)


This is something I am trying but can not get it to work:


                function cmp($a, $b) {
    		    return $a->attachment_id - $b->attachment_id;
		}

                $a	= $p->get_attached_array();
		$newarray = array();
				
		foreach($a as $attach_type) { //$attach_type = sub arrays to main array ($a)...image array, vieoembed array, etc.
			$newarray[] = usort($attach_type,'cmp');
		}
		
		print_r($newarray);

You could do something like this:

<?php

$image = array(
	0 => array('title' => 'image1.jpg', 'attachment_id' => 699),
	1 => array('title' => 'image2.jpg', 'attachment_id' => 675),
	2 => array('title' => 'image21.jpg', 'attachment_id' => 693),
);

function imageSort($x, $y) {
	return ($x['attachment_id'] > $y['attachment_id']);
}

// Print the array as is:
echo '<h2>Array As Is</h2><pre>' . print_r($image, 1) . '</pre>';

// Sort by score:
uasort($image, 'imageSort');
echo '<h2>Array Sorted By ID:</h2><pre>' . print_r($image, 1) . '</pre>';

I think it would be easier to do an array_shift or array_pop to do the other sorting: http://us2.php.net/manual/en/function.array-shift.php

Maybe a solution from sitepoint forums might help.

function cmp($a, $b)
{
    return strcmp($a->attachment_id, $b->attachment_id);
}

usort($your_data, "cmp");