0

Wonder if anyone can help.

I have a two dimensional array which has a number of job items in eg;

$portfolioItems [$i] = array('job' => $extra_job->field_value,
      'mediaType' => $media_type->field_value, 'default' => $default->field_value
      );

I have simplified the array to show the elements I'm trying to sort.

Basically this list eventually populates the jfredcarousel I'm using with thumbnails/data etc.

This all works fine but what I'd like to do is have a specific order as follows;

if the media type has the text 'hero' in it (will just be one instance) then shove this right to the front.

Then the next items need to be some job items that have been identified as a 'default' set of items. so I'll probably set a flag somewhere that identifies a job number as being 'default' and check like with the 'hero' process.

So it would be 'hero' item, then the job number items defined as 'default', the rest would just drop in order after that (i'm already sorting the items by job number).

What's the best way to go about sorting an exsiting two dimensional array ? I had a look at shift/unshift etc but couldn't achieve what I wanted - I'm now looking at just duplicating the array then checking for these conditions one by one so the new array looks right. then destroying the old array.

Any thoughts appreciated

Thanks

2 Answers 2

2

Use uasort and write your own comparison function which embodies those rules you want.

Something like:

function cmp($a, $b) {
    if (strpos($a['mediaType'], 'hero') !== false && strpos($b['mediaType'], 'hero') === false) {
        return 1;
    } else if (strpos($a['mediaType'], 'hero') === false && strpos($b['mediaType'], 'hero') !== false) {
        return -1;
    } else if ($a['default'] == 1 && $b['default'] != 1) {
        return 1;
    } else if ($a['default'] != 1 && $b['default'] == 1) {
        return -1;
    } else {
        return 1;
    }
}

uasort($portfolioItems, 'cmp');
Sign up to request clarification or add additional context in comments.

3 Comments

Cheers - Just a question though with uasort maintaining the index number, when I foreach through this array later to stick into an XML file - won't it be back in the index order ?
No, the purpose of uasort is to sort (change the order of) the array. Don't confuse indexing with ordering, as they aren't always the same. If you don't need to maintain the index associations you can use usort instead, though.
Thanks Dan - I'll have a look at getting this going. Am I right in saying as this is a Two Dimensional array, I'm going to need to stick this function in a foreach for every element in the whole array ? eg uasort($portfolioItems[$count], 'cmp');
0

Something like that maybe? :)

$finalItems = array();

for($i = 0; i < count($portfolioItems); $i++){
       if($portfolioItems[$i]['media_type'] == 'hero'){
         reindex($finalItems, 0);
         $finalItems[0] = $portfolioItems[$i];
       }
       else if($portfolioItems[$i]['default'] == true){
         reindex($finalItems, 1);
         $finalItems[1] = $portfolioItems[$i];
       }

       else {
            if($i != 0 && $i != 1){
                  reindex($finalItems, $i);
                  $finalItems[$i] = $portfolioItems[$i];
            }
       }
}

function reindex(&$arr, $modifiedPosition){

 for($i = 0; $i < count($arr); $i++){
  if($modifiedPosition <= $i){
   $arr[$i+1] = $arr[$i]; 

    if($i == $modifiedPosition){
     unset($arr[$i]);
    }

  }

 }

}

3 Comments

didn't test it though, just tell if it dosn't work, will try to fix it.
Well I updated it, previous clearly wouldn't work, that one should.
hi, that works great thanks. I stuck this on the end also as the order was still as orginal. ksort($finalItems);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.