简体   繁体   English

在PHP中对二维数组进行排序

[英]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. 基本上,此列表最终会在缩略图/数据等中填充我正在使用的jfredcarousel。

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. 如果媒体类型中包含文本“ hero”(将只是一个实例),则将此权利推到最前面。

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. 使用uasort并编写自己的比较函数,以体现您想要的那些规则。

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

  }

 }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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