[英]sort multidimension array by its keys and then by its value
I want to sort two dimension array but its keys and its values. 我想对二维数组进行排序,但要排序其键和值。 Array is like this
数组是这样的
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
)
)
I have sorted array of array by its keys but what I want to do is I want to array like this 我已经按其键对array数组进行了排序,但是我想做的就是这样
<?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
)
)
?>
I have sorted by its keys I am not able to sort by value please help what I have to do 我已经按其键排序了,我无法按值排序,请帮忙我必须做的
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;
}
I want dir in key and files in value. 我想要密钥中的目录和值中的文件。
Solve: 解决:
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;
}
Array will look like this and i want to sort array [transport]=> imagem2.jpg ' value not [0] this index. 数组看起来像这样,我想对数组[transport] => 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
)
)
Use this : 用这个 :
call a sorting function and store the return sorted array in a variable $sorted_array 调用排序函数并将返回的排序数组存储在变量$ sorted_array中
$sorted_array = sortArray($result, $p_sort_field );
Sorting function : 排序功能:
<?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;
}
}
?>
In $result pass your actual array and in $p_sort_field pass the field name you want to sort. 在$ result中传递您的实际数组,在$ p_sort_field中传递您要排序的字段名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.