Glob array by number value - php

I'm using glob to array my sub directories
<?php
$items = glob("../albums/*", GLOB_ONLYDIR);
foreach($items as $item) {
echo "$item\n ";
}
?>
he orders them by the alphabet, i'd like to order them by numbers
if i got sub-directories named 1 , 3 , 5 , 10
the will be arranged like this : 10, 1, 3, 5
i want them to be like this 1, 3, 5, 10
is there an option? thanks
EDIT:
now i found natsort($files);
and when im using it with:
$items = array_slice(glob('albums/*', GLOB_ONLYDIR), 0, 4);
natsort($items);
and my folders are 995-1000
it gives me this array : 995, 996, 997, 1000

Take a look at natsort()

use natsort to sort the array in natural order:
natsort($array);

You could sort the array after the glob:
sort($items, SORT_NUMERIC);

You can achieve this by using array sorting natsort() like this;
$items = glob("../albums/*", GLOB_ONLYDIR);
natsort($items);
Now if print this array then you get the proper result by this.
print_r($items);

Related

Making tops from arrays

I have got two arrays, first contains some nicknames, and second contains integer values. Like "felenot", "argold", "pilina" and 130, 10, 66.
I need to put numbers in descending order, and then make string like this: felenot - 130.
Any ideas?
use array_multisort
<?
$arr1 = ["felenot", "argold", "pilina"];
$arr2 = [130, 10, 66];
array_multisort($arr2, SORT_DESC, $arr1);
echo "{$arr1[0]} - {$arr2[0]}";
demo

How can I convert 1D array from numbers to 2D array?

I have an array from numbers like this :
$array = array (1,2,3,4,5,6,7,8,9,10,11,12);
I tried to convert it to 2D using two for loops but i failed. Is it better to try with foreach loop , or it can be done with for-s ?
I wanna make something like this:
$array = array (array (1,2,3,4), array(5,6,7,8), array(9,10,11,12));
The reason I'm posting an answer is that a question like this and the availability of array_chunk() may not be very intuitive:
$array = array_chunk($array, 4);

usort() - Sorting an array in PHP

I'm new here in.
I have a quick question:
I have an array that looks like so -
$array = array(413, 913, 5);
foreach($array as $arr){
echo $arr . '<br />';
}
And the output is -
413
913
5
How can I use the usort() function to make the script output -
913
5
413
If you could help me that would be great! Thank you.
If you just want to sort reversed by digits from left to right, ie. as though they are strings
Just use a normal sort as string values:
$array = array(413, 913, 5);
rsort($array, SORT_STRING);
var_dump($array);

PHP sorting of multi-dimensional array by price value

Say I have a multi-dimensional array set up as follows:
$matrix[1][1]=4.54; $matrix[2][1]="apples"; $matrix[3][1]="coles";
$matrix[1][2]=7.2140230; $matrix[2][2]="apples"; $matrix[3][2]="safeway";
$matrix[1][3]=15.56; $matrix[2][3]="oranges"; $matrix[3][3]="coles";
$matrix[1][4]=2.34; $matrix[2][4]="bananas"; $matrix[3][4]="safeway";
$matrix[1][5]=27.98; $matrix[2][5]="grapes"; $matrix[3][5]="coles";
$matrix[1][6]=17.68493403; $matrix[2][6]="oranges"; $matrix[3][6]="safeway";
And I wish to re-arrange by the pricing information which I've stored under the first 1st column, so that the new order of $matrix would be:
$matrix[1][1]=2.34; $matrix[2][1]="bananas"; $matrix[3][1]="safeway";
$matrix[1][2]=4.54; $matrix[2][2]="apples"; $matrix[3][2]="coles";
$matrix[1][3]=7.2140230; $matrix[2][3]="apples"; $matrix[3][3]="safeway";
$matrix[1][4]=15.56; $matrix[2][4]="oranges"; $matrix[3][4]="coles";
$matrix[1][5]=17.68493403; $matrix[2][5]="oranges"; $matrix[3][5]="safeway";
$matrix[1][6]=27.98; $matrix[2][6]="grapes"; $matrix[3][6]="coles";
What would be the best way to achieve this? I've read other questions about sorting multi-dimensional arrays but have had trouble implementing because those examples seemed to have associative arrays with keys and elements, whereas I am just using the different numbers to store each piece of data. I would prefer not to change the way I am storing data in the array as the actual script is quite long and complex and so this would involve a lot of re-work.
I am completely new to PHP so my apologies if I am missing something obvious here. Thanks for your help.
EDIT: Thanks everyone for your advice, scessors code is exactly what I needed. Halfer - to your 1st question - Yes, 2nd post - good point, I will implement this. Thanks again everyone!
If it's no problem that the sorted array begins with zero, you can use array_multisort:
array_multisort(
$matrix[1], SORT_ASC, SORT_NUMERIC,
$matrix[2], SORT_ASC, SORT_STRING,
$matrix[3], SORT_ASC, SORT_STRING
);
Also see my example.
I believe that phps array_multisort will do it for you:
Heres an example from docs:
<?php
$ar1 = array(10, 100, 100, 0);
$ar2 = array(1, 3, 2, 4);
array_multisort($ar1, $ar2);
var_dump($ar1);
var_dump($ar2);
?>
In this example, after sorting, the first array will contain 0, 10, 100, 100. The second array will contain 4, 1, 2, 3. The entries in the second array corresponding to the identical entries in the first array (100 and 100) were sorted as well.
From: http://php.net/manual/en/function.array-multisort.php
Using my_uksort provided by Adam Backstrom
<?php
function my_uksort($a, $b) {
global $matrix;
asort($matrix[1]);
return $matrix[1][$a] < $matrix[1][$b] ? -1 : 1;
}
uksort($matrix[2], 'my_uksort');
uksort($matrix[3], 'my_uksort');
print_r($matrix);
?>
DEMO

PHP Arrays : How to Find the Lowest/Highest Value

I have two array like this :
$array1 = array(1,1,2,3,3,4,5); //remember that i have two '1' value in this array
$array2 = array($url1, $url2, $url3, $url4, $url5, $url6);
I wish to find the lowest/highest value in $array1 then link to $url1/$url5 like this :
1 or 5
How I can make this happen using PHP? Any help would be greatly appreciated
Thanks
Use the max() & min() function
max — Find highest value
min — Find lowest value
Example code:
$max = max($array);
$min = min($array);
If the array is already sorted, use $array2[0] and $array2[count($array2)-1].
If it's not already sorted, you can use this to sort the arrays.
array_multisort($array1, SORT_NUMERIC, $array2);
$lowest = $array2[0];
$highest = $array2[count($array2)-1];
As far as I understand you need this 2 functions: max, min
try this
$maxValueKeys = array_keys($array1, max($array1)); // Your min value indexes
$minValueKeys = array_keys($array1, min($array1)); // Your max value indexes
But it isn't an associative array

Categories