Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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
            )

    )

)
share|improve this question

marked as duplicate by Jon, deceze, Bill the Lizard 21 hours ago

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2 Answers

You can use usort and give a custom comparsion function.

share|improve this answer
This is something I am trying but can not get it to work: Code: 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); – Martin S 23 hours ago
I'm not sure that i correctly understand your question, but if you want your array to be sorted first by the "type" (image, videoembed) and then by the attachment id, you have to do two sorts first you use usort to sort the sub arrays and then concat them and sort by the key(ksort). I'm not sure that it can be done with one sort, but maybe a twisted usort with some global state can do it. – erdeszt 23 hours ago

Using usort as below:

function cmp( $a, $b ) { 
  if(  $a->attachment_id ==  $b->attachment_id){ return 0 ; } 
  return ($a->attachment_id< $b->attachment_id) ? -1 : 1;
} 

for($i=0, $i<len($imagearray), $i++)
{
  usort($imagearray,'cmp');//assign image array to $imagearray
}
share|improve this answer
[image] is a subarray, of the parent array. so it is: mainarray->image array->objects->attachment_id mainarray->videoembed array->objects->attachment_id so wanting to sort all the objects under mainarray->image array by attachment id and the same for videoembed array, etc while keeping them under mainarray..if that makes sense. – Martin S yesterday
You can try as the code i gave above is not tested . I directly wrote here. Please inform the code that works. – Afroz Alam yesterday
This is what I have tried, similar, but can not get it to work: $a = $p->get_attached_array(); (we get the array here) $newarray = array(); function cmp($a, $b) { return ($a->attachment_id< $b->attachment_id) ? -1 : 1; } foreach($a as $attach_type) { $newarray[] = usort($attach_type,'cmp'); } print_r($newarray); – Martin S 23 hours ago

Not the answer you're looking for? Browse other questions tagged or ask your own question.