Reputation: 83
I want to sort two dimension array but its keys and its values. Array is like this
[
['Transport' => 'imagem3.png'],
['Transport' => 'imagem2.png'],
['Transport' => 'imagem1.png'],
['First' => 'dscn2439.jpg'],
['First' => 'dscn2454.jpg'],
['First' => '06052010282.jpg'],
['First' => 'dscn2357.jpg'],
['Manufacture' => '120140220_191807.jpg'],
['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
[
['Transport' => 'imagem1.png'],
['Transport' => 'imagem2.png'],
['Transport' => 'imagem3.png'],
['First' => 'dscn2439.jpg'],
['First' => 'dscn2454.jpg'],
['First' => 'dscn2357.jpg'],
['First' => '06052010282.jpg'],
['Manufacture' => '120140220_191807.jpg'],
['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;
}
Upvotes: 0
Views: 86
Reputation: 48031
It appears that you don't want natural sorting. You want to preserve the original 2nd level key order in terms of grouping by keys, then you want to sort by filenames which start with a letter then filenames which do not start with a letter, then split any remaining ties by sorting as whole strings.
To reduce the overall computational complexity (improve performance), make a single pass over the input array and calculate all metrics for sorting, then make a call of array_multisort()
using those metrics. Demo
$keyPriority = [];
$valuePriority = [];
$counter = 0;
foreach ($array as $row) {
$k = key($row);
$keys[$k] ??= ++$counter;
$keyPriority[] = $keys[$k];
$valuePriority[] = !ctype_alpha($row[$k][0]);
}
array_multisort($keyPriority, $valuePriority, $array);
var_export($array);
Upvotes: 0
Reputation: 1649
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.
Upvotes: 0
Reputation:
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.
Upvotes: 1