通过其键然后通过其值对多维数组进行排序

时间:2014-09-24 19:11:29

标签: php arrays sorting

我想对二维数组进行排序,但是它的键及其值。 数组就像这样

    Array
(
    [0] => Array
        (
            [Transport] => imagem3.png
        )

    [1] => Array
        (
            [Transport] => imagem2.png
        )

    [2] => Array
        (
            [Transport] => imagem1.png
        )

    [3] => Array
        (
            [First] => dscn2439.jpg
        )

    [4] => Array
        (
            [First] => dscn2454.jpg
        )

    [5] => Array
        (
            [First] => 06052010282.jpg
        )

    [6] => Array
        (
            [First] => dscn2357.jpg
        )

    [7] => Array
        (
            [Manufacture] => 120140220_191807.jpg
        )

    [8] => Array
        (
            [Manufacture] => 20140220_191429.jpg
        )
)

我已经通过键对数组进行了排序,但我想做的是我想像这样的数组

<?php Array
(
[0] => Array
    (
        [Transport] => imagem1.png
    )

[1] => Array
    (
        [Transport] => imagem2.png
    )

[2] => Array
    (
        [Transport] => imagem3.png
    )

[3] => Array
    (
       [First] => dscn2357.jpg
    )

[4] => Array
    (
        [First] => dscn2439.jpg
    )

[5] => Array
    (
         [First] => dscn2454.jpg
    )

[6] => Array
    (
       [First] => 06052010282.jpg
    )

[7] => Array
    (
        [Manufacture] => 120140220_191807.jpg
    )

[8] => Array
    (
        [Manufacture] => 20140220_191429.jpg
    )
 )
 ?>

我按其键排序我无法按值排序请帮助我必须做的事

function getListPortfolio($params){

        $dir ='./images/portfolio/';
        // Open a directory, and read its contents
        $folderArray=array();
        $fileArray=array();
        $extArray=array('.jpg', '.jpeg', '.png', '.gif');
        if (is_dir($dir)){
          if ($dh = opendir($dir)){
            while (($subdir = readdir($dh)) !== false){             
                if(is_dir($dir.$subdir)){
                    if(!($subdir=='.' || $subdir=='..')){
                         $folderArray[]=$subdir;
                    }
                }
            }

            arsort($folderArray);
            foreach($folderArray as $key=>$value){
                if($dhSub = opendir($dir.$value)){
                     while (($files = readdir($dhSub)) !== false){
                        $fileExists=$dir.$value.'/'.$files;
                        if(exif_imagetype($fileExists)){
                            if(is_file($fileExists)){
                                $fileArray[][$value]=$files;                                        
                            }
                        }
                     }
                }
            }
            closedir($dh);
          }
        }
        return $fileArray;
    }

我想要密钥和文件中的dir值。

解:

function getListPortfolio($params){

        $dir ='./images/portfolio/';
        // Open a directory, and read its contents
        $folderArray=array();
        $fileArray=array();
        $data=array();
        $extArray=array('.jpg', '.jpeg', '.png', '.gif');
        if (is_dir($dir)){
          if ($dh = opendir($dir)){
            while (($subdir = readdir($dh)) !== false){             
                if(is_dir($dir.$subdir)){
                    if(!($subdir=='.' || $subdir=='..')){
                         $folderArray[]=$subdir;
                    }
                }
            }

            arsort($folderArray);
            foreach($folderArray as $key=>$value){
                if($dhSub = opendir($dir.$value)){
                     while (($files = readdir($dhSub)) !== false){
                        $fileExists=$dir.$value.'/'.$files;
                        if(exif_imagetype($fileExists)){
                            if(is_file($fileExists)){
                                $fileArray[]=$files;                                        
                            }
                        }
                     }
                     asort($fileArray);
                     foreach($fileArray as $index=>$filename){
                         $data[][$value]=$filename;
                     }

                }
            }
            closedir($dh);
          }
        }
        return $fileArray;
    }

数组看起来像这样,我想排序数组[transport] =&gt; imagem2.jpg '值不是[0]这个索引。

Array
( [0] => Array ( [Transport] => imagem1.png )

[1] => Array
    (
        [Transport] => imagem2.png
    )

[2] => Array
    (
        [Transport] => imagem3.png
    )

[3] => Array
    (
       [First] => dscn2357.jpg
    )

[4] => Array
    (
        [First] => dscn2439.jpg
    )

[5] => Array
    (
         [First] => dscn2454.jpg
    )
)

2 个答案:

答案 0 :(得分:1)

使用此:

调用排序函数并将返回的排序数组存储在变量 $ sorted_array

$sorted_array = sortArray($result, $p_sort_field );

排序功能:

<?php

function sortArray($arrData, $p_sort_field, $p_sort_type = false )
{
 if(!empty($arrData))
 {
  foreach($arrData as $data)
  {
   $newData [] = $data;
  }
  for($i=0; $i<count($newData); $i++)
  {                       
    $ar_sort_field[$i]=$newData[$i][$p_sort_field];
  }
  array_multisort($ar_sort_field, ($p_sort_type ? SORT_DESC : SORT_ASC), $newData);   
  return $newData;
 }
}

?>

$ result 中传递您的实际数组,并在 $ p_sort_field 中传递您要排序的字段名称。

答案 1 :(得分:0)

所以你想按值对数组进行排序。你怎么不用usort这样:

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

usort($arr, 'cmp');

print_r($arr);

$ arr是您要排序的数组。