vartateInfo vartateInfo - 17 days ago 6
PHP Question

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

<?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
)
)

Answer

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.

Comments