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
<?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.
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 = 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.
So you want to sort your array by value. What don't you use usort like this:
function cmp($a, $b)
{
if ($a == $b)
return 0;
return ($a < $b) ? -1 : 1;
}
usort($arr, 'cmp');
print_r($arr);
$arr being the array you want to sort.
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.