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
Related
There is an array, the count of the element is unknown,like this:
$arr=['a','m','q','y',....'b','f','n','s'];
How to get the second-to-last element in PHP?
You can use array_slice() like this:
<?php
$arr=['a','m','q','y','b','f','n','s'];
echo array_slice($arr, -2, 1)[0];
Output:
n
Note: this will work regardless of the array type: indexed or associative. So even if the keys are not 0, 1, 2, 3, etc... then this would still work.
Since there are no defined keys, it's a little easier:
$second_to_last = $arr[count($arr) - 3];
I think this is the answer;
$arr[count($arr)-3]
I need to know the number of different values in an array. For example if it contains (5, 5, 5) then I should get 1. If it contains (4, 4, 2, 2) I should get 2. If (5, 5, 5, 5, 2, 2, 1, 5) then 3.
Is there a function that can achieve that directly in PHP?
echo count(array_unique($arr));
you can use array_unique which returns unique elements of array (result is also array type) and then count on result
Two simple ways to achieve that:
$diff_count = count(array_unique($input_arr));
$diff_count = count(array_flip($input_arr)); <-- recommended (performance)
array_unique() :
Removes duplicate values from an array - you can add adittional flags such as:
SORT_REGULAR - compare items normally (don't change types)
SORT_NUMERIC - compare items numerically
SORT_STRING - compare items as strings
SORT_LOCALE_STRING - compare items as strings, based on the current locale.
array_flip():
Exchanges all keys with their associated values in an array - so duplicates keys are overwritten and you get a final array with the unique values as keys.
Flipping it twice will result in an array with unique values and the highest keys there were in the original input array.
More reading:
array_unique.
array_flip.
Benchmark:
Since the array_flip uses only O(n) exactly its much faster - using an array of 10,000 numbers as an input:
array_unique + count: ~28.615 ms.
array_flip + count: ~0.5012 ms. (winner)
Is there a function that can achieve that directly in PHP?
The closest one would be array_unique() but here is a simple function to achieve what you are looking for:
$arr = array(5,5,5,5,2,2,1,5);
function array_diff_values( $array ) {
return count( array_unique($array) );
}
$count = array_diff_values( $arr );
result of $count is 3
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
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);
If I have this array:
<?php
$array[0]='foo';
$array[1]='bar';
$array[2]='foo2';
$array[3]='bar3';
?>
And I want to delete the second entry ($array[1]), but so that all the remaining entries' indexes automatically get adjusted, so the next 2 elements whose indexes are 2 and 3, become 1 and 2.
How can this be done, or is there any built in function for this?
There are several ways to do that. One is to use array_values after deleting the item to get just the values:
unset($array[1]);
$array = array_values($array);
var_dump($array);
Another is to use array_splice:
array_splice($array, 1, 1);
var_dump($array);
You can use array_splice(). Note that this modifies the passed array rather than returning a changed copy. For example:
$array[0] = 'foo';
//etc.
array_splice($array, 1, 1);
print_r($array);
I always use array_merge with a single array
$array = array_merge($array);
print_r($array);
from php.net:
http://us.php.net/array_merge
If only one array is given and the array is numerically indexed, the keys get reindexed in a continuous way.