PHP sorting of multi-dimensional array by price value - php

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

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

Sorting PHP Randomised numbers by size

So I've got this:
$h = $user_goals;
while($h > 0) {
randomScorer();
$minute = rand(0,90);
echo "(".$minute.")<br>";
$h--;
Basically, what it does is, $user_goals, has a load of factors drawn into it and creates a number, between 0-5, and this information is used to generate the times of the goals, using the above PHP function.
It's working, it's brilliant, etc. However, the numbers are appearing in random order in which they are generated and so I was wondering:
Is there any way to sort these numbers?
Would I put them into an array during this iteration methodology, and then sort the array by the number's value?
Any help is greatly appreciated!
That is why PHP provides us Sort functions. Have a look here.
<?php
$fruits = array("lemon", "orange", "banana", "apple");
sort($fruits);
?>
Since your array is NUMERIC, you need to use the FLAG along with the sort function.
sort($goals, SORT_NUMERIC);
print_r($goals);
Same idea, using sort() but also uses range and array_walkto set up your array a little closer to how you already do it:
$goal_array = range(1, $user_goals); // Warning, assumes $user_goals is number
array_walk($goal_array, function(&$goal) {
randomScorer();
$goal = rand(0,90);
});
sort($goal_array, SORT_NUMERIC);

PHP array sort()

Im stuck on a sorting problem, I have an array with 10 numbers (1-10) and I need to sort the in the following way where 10 would come after 1, for example...
desired outcome
$arr['a1','a10','a2','a3','a4','a5','a6','a7','a8','a9'];
actual outcome
$arr['a1','a2','a3','a4','a5','a6','a7','a8','a9','a10'];
sort($arr);
$arr['a10','a1','a2','a3','a4','a5','a6','a7','a8','a9'];
I don't know the name of this type of sorting or how to perform it, if anyone could help me it would much appreciated.
NOTE: the numbers are part of a string
Try sort($arr,SORT_STRING) to explicitly treat the input as strings.
EDIT: Now that you've given your actual strings, try this:
usort($arr,function($a,$b) {
$a = explode("=",$a);
$b = explode("=",$b);
return $a[0] == $b[0] ? strcmp($a[1],$b[1]) : strcmp($a[0],$b[0]);
});
Sure, you want to sort alphabetically, not numerically.
sort($arr, SORT_STRING);
ref: http://php.net/manual/en/function.sort.php
You can change the behaviour of sort with it's second parameter.
Try this:
sort($arr, SORT_STRING);

merging two arrays and keep duplicate values

Is there a php function to merge 2 arrays and keep duplicates?
Like:
$a=array('a','b','c');
$b=array('b','c','b');
array_merge2($a,$b);
//result: array('a','b','c','b','c','b');
thanks
add1:
what the... i previously tested array_merge and it didn't keep duplicate values =/
In the examples for array_splice it shows how to add the contents of one array onto the end of another. Just replace the last parameter with your array.
array_splice($input, count($input), 0, array($x, $y));
So in your example:
$a=array('a','b','c');
$b=array('b','c','b');
array_splice($a, count($a), 0, $b);
array_merge keeps the duplicates.
http://codepad.org/XGcMAi3z

Deleting from PHP array and having indexes adjusted

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.

Categories