简体   繁体   中英

Sorting a 2 dimensional array in PHP

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

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');

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]);
    }

  }

 }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM