How to sort string index of php array - php

I am interested to sort php array index key in ascending order,
I have this array
(
[462 1 5.300] => 1
[462 9 4.900] => 1
[462 9 4.300] => 1
[462 2 4.800] => 1
[462 6 4.700] => 1
[462 7 4.900] => 1
[462 7 4.700] => 1
[462 8 4.500] => 1
[462 3 4.500] => 1
[462 4 4.700] => 1
[462 3 4.700] => 1
[462 5 4.500] => 1
)
I expect output to be like this after sort
(
[462 1 5.300] => 1
[462 2 4.800] => 1
[462 3 4.500] => 1
[462 3 4.700] => 1
[462 4 4.700] => 1
[462 5 4.500] => 1
[462 6 4.700] => 1
[462 7 4.700] => 1
[462 7 4.900] => 1
[462 8 4.500] => 1
[462 9 4.300] => 1
[462 9 4.900] => 1
)
I want to sort first 3 index which is used as string is it possible ? I have 3 more column in array but I am showing only 3 columns here..

A normal ksort() can do the trick, but if the sorting is to be performed in multiple orders (different orders for different parameter values), this wouldn't work.
You can achieve this with array_multisort(). First you create three (or more, depending on the number of parameters you have) arrays - each containing the parameter values. Then simply pass it to array_multisort() with the corresponding sort flag:
$param1 = $param2 = $param3 = array();
foreach ($data as $key => $value) {
$parts = explode(' ', $key);
$param1[] = $parts[0];
$param2[] = $parts[1];
$param3[] = $parts[2];
}
array_multisort($data, $param1, SORT_ASC, $param2, SORT_ASC, $param3, SORT_ASC);
If you want the sorting to be performed in descending order, then you can use SORT_DESC instead. See the documentation for a list of available sort flags.
Demo

The function usort() would be the answer, but you need to sort by key, not by value. So you'd have to use uksort().
Tiny help for you (not tested):
<?php
function cmp($a, $b)
{
$aParts = explode(' ',$a);
$bParts = explode(' ',$b);
if($aParts[0] !== $bParts[0]) {
return ($aParts[0] >= $bParts[0]) ? -1 : 1;
}
if( (int) $aParts[1] !== (int) $bParts[1]) { // compare second parts as integer
return ($aParts[1] >= $bParts[1]) ? -1 : 1;
}
if( (float) $aParts[2] !== (float) $bParts[2]) {
return ($aParts[2] >= $bParts[2]) ? -1 : 1;
}
return 0;
}
$a = array(
[462 1 5.300] => 1
[462 9 4.900] => 1
[462 9 4.300] => 1
[462 2 4.800] => 1
[462 6 4.700] => 1
[462 7 4.900] => 1
[462 7 4.700] => 1
[462 8 4.500] => 1
[462 3 4.500] => 1
[462 4 4.700] => 1
[462 3 4.700] => 1
[462 5 4.500] => 1
);
uksort($a, "cmp");
foreach ($a as $key => $value) {
echo "$key: $value\n";
}

In order to get the output you are showing, you can use the ksort() function -http://www.php.net/manual/fr/function.ksort.php.
<?php
$test = array(
"462 1 5.300" => 1,
"462 9 4.900" => 1,
"462 9 4.300" => 1,
"462 2 4.800" => 1,
"462 6 4.700" => 1,
"462 7 4.900" => 1,
"462 7 4.700" => 1,
"462 8 4.500" => 1,
"462 3 4.500" => 1,
"462 4 4.700" => 1,
"462 3 4.700" => 1,
"462 5 4.500" => 1
);
print_r($test);
ksort($test);
print_r($test);
?>

Related

Sort array of months with laravel collection

My array is
$array = [
1 => 0
2 => 0
3 => 0
4 => 0
5 => 0
6 => 0
7 => 2
8 => 0
9 => 0
10 => 1
11 => 0
12 => 1
];
As a result i want it to be
$array = [
10 => 1
11 => 0
12 => 1
1 => 0
2 => 0
3 => 0
4 => 0
5 => 0
6 => 0
7 => 2
8 => 0
9 => 0
];
And my logic for now is
$sorted = collect($array)
->sortBy(function ($count, $month) {
return $month <= 9;
});
But the result is not what I expected :(
Basically i want last index to be current month and so on back.
Please help me!
You can do it like this:
$sorted = collect($array)->sortBy(function ($count, $month) {
$currentMonth = (int) \Carbon\Carbon::now()->month;
return ($month + (12 - $currentMonth - 1)) % 12;
});
You can function form php that
krsort();
krsort can sort associative arrays in descending order, according to the key. So
ksort($array);

How to make a graphic using numbers with PHP [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I need to break a number in a table field and turn it into an array.
input_table
+------+---------+
| id | number |
+------+---------+
| 1 | 7 |
| 2 | 8 |
+------+---------+
for the id 1 (which has 7 number) the output what I need in php is:
1 2 3 4 5 6 7
2 3 4 5 6
3 4 5
4
How to do this?
for ($q=1; $q <= $obj->number ; $q++) {
echo "$q";
//This only turn 1, 2, 3, 4, 5, 6, 7
}
try something like this.
$set = array( 7, 8 );
echo '<pre>';
foreach( $set as $number ){
//assuming number is your INT
$array = range( 1, $number );
while( count( $array ) ){
echo "\n";
var_export( $array );
//remove first element
array_shift( $array );
//remove last element
array_pop($array);
}
}
Outputs:
For 7
array (
0 => 1,
1 => 2,
2 => 3,
3 => 4,
4 => 5,
5 => 6,
6 => 7,
)
array (
0 => 2,
1 => 3,
2 => 4,
3 => 5,
4 => 6,
)
array (
0 => 3,
1 => 4,
2 => 5,
)
array (
0 => 4,
)
For 8
array (
0 => 1,
1 => 2,
2 => 3,
3 => 4,
4 => 5,
5 => 6,
6 => 7,
7 => 8,
)
array (
0 => 2,
1 => 3,
2 => 4,
3 => 5,
4 => 6,
5 => 7,
)
array (
0 => 3,
1 => 4,
2 => 5,
3 => 6,
)
array (
0 => 4,
1 => 5,
)
I'll leave it to you to build a multi-dimensional array out of that.
If you just want the output, than use echo implode(' ', $array ); in place of var_export(). Such as this:
$set = array( 7, 8 );
echo '<div style="text-align:center">';
foreach( $set as $number ){
//assuming number is your INT
$array = range( 1, $number );
while( count( $array ) ){
echo implode(' ', $array );
//remove first element
array_shift($array);
//remove last element
array_pop($array);
echo '<br>';
}
echo '<br>';
}
echo '</div>';
Outputs:
1 2 3 4 5 6 7
2 3 4 5 6
3 4 5
4
1 2 3 4 5 6 7 8
2 3 4 5 6 7
3 4 5 6
4 5
<?php
$n = 7; //or whatever you want
echo '<div style="text-align:center">';
for($i=0;$i<=round($n/2,0);$i++){
for($j=$i; $j<$n-$i;$j++){
echo ($j+1).' ';
}
echo "<br />\n";
}
echo '</div>';

Sorting with arsort not stable [duplicate]

This question already has answers here:
How to have a stable sort in PHP with arsort()?
(7 answers)
Closed 9 years ago.
There is strange issue in php asort, arsort.
I am taking example of arsort
Case1
$a = array(
1 => 2,
2 => 1,
3 => 2,
4 => 1
);
arsort($a);
var_dump($a);
Output:
array(4) {
[3] =>
int(2)
[1] =>
int(2)
[4] =>
int(1)
[2] =>
int(1)
}
Here at index (3,1) and (4,2) are sorted in descending order because at index 3 and 1 values are same. Same for index 4 and 2.
Case2
$a = array(
1 => 2,
2 => 1,
3 => 2
);
arsort($a);
var_dump($a);
Output:
array(3) {
[1] =>
int(2)
[3] =>
int(2)
[2] =>
int(1)
}
Here at index (3,1) are sorted in ascending order and still at index 3 and 1 values are same.
Is there any solution for this issue? As I want that ordering should be certain. Like sort in either descending or ascending order if value at some indices are same.
According to the PHP documentation:
If any of these sort functions evaluates two members as equal then the order is undefined (the sorting is not stable).
You can't know exactly which behaviour is correct, with testing just with 2 elements. Here's an array with multiple elements (odd and even).
even number:
<?php
$a = array(
1 => 2,
2 => 1,
3 => 2,
4 => 1,
5 => 2,
6 => 1,
7 => 2,
8 => 1,
9 => 2,
10 => 1,
11 => 2,
12 => 1
);
arsort($a);
var_dump($a);
Result:
array (size=12)
1 => int 2
7 => int 2
5 => int 2
11 => int 2
9 => int 2
3 => int 2
10 => int 1
12 => int 1
6 => int 1
2 => int 1
4 => int 1
8 => int 1
odd number
<?php
$a = array(
1 => 2,
2 => 1,
3 => 2,
4 => 1,
5 => 2,
6 => 1,
7 => 2,
8 => 1,
9 => 2,
10 => 1,
11 => 2,
12 => 1,
13 => 2
);
arsort($a);
var_dump($a);
Result
array (size=13)
9 => int 2
11 => int 2
13 => int 2
1 => int 2
7 => int 2
3 => int 2
5 => int 2
12 => int 1
2 => int 1
4 => int 1
8 => int 1
6 => int 1
10 => int 1
The question is now, where did it add the 13th element (=2)? it's added just after the 11th element and before the first element...this means that there's no rule here. (At least according to what we see).
We can't say that it follows any rule with testing with just 2 variables like what you did, because you saw (1,3) and you presumed that it's sorted by key. Which is apparently not the case with multiple variables.

Converting a One-Dimensional Array into a Nested Array Using Relationship Keys

Say I have an array with keys representing id and values representing parent:
4 => 0
2 => 0
5 => 2
6 => 5
8 => 0
9 => 0
10 => 8
12 => 0
13 => 0
14 => 0
18 => 7
19 => 18
20 => 19
21 => 20
22 => 21
23 => 22
24 => 23
28 => 20
7 => 5
You could also read this as an object:
{
id : 4,
parent : 0
} // etc...
The multidimensional array I'd want to achieve from this would be:
4 => 0
2 => 5
=> 6
=> 7
=> 18
=> 19
=> 20
=> 21
=> 22
=> 23
=> 24
=> 28
8 => 10
9 => 0
12 => 0
13 => 0
14 => 0
How would I go about doing this?
If you write a little helper function to rework your data to a structure similar to:
$input = array(
array('id' => '4', 'parent' => '0'),
// ...
);
which could be achieved with something like:
$data = array_map(function ($entry) {
list($id, $parent) = array_map('trim', explode('=>', $entry));
return array(
'id' => $id,
'parent' => $parent
);
}, explode("\n", $data));
you could then use a function I used in a similar question:
function flatToNested($d, $r = 0, $p = 'parent', $k = 'id', $c = 'children') {
$m = array();
foreach ($d as $e) {
isset($m[$e[$p]]) ?: $m[$e[$p]] = array();
isset($m[$e[$k]]) ?: $m[$e[$k]] = array();
$m[$e[$p]][] = array_merge($e, array($c => &$m[$e[$k]]));
}
return $m[$r];
}
to produce a nested array with:
$nested = flatToNested($data);
demo: http://codepad.viper-7.com/HAZxaA

Order Array by 2nd value

I have an array as follows
Array
(
[1845267] => 2
[1845256] => 2
[1845260] => 2
[33636] => 1
[67376] => 2
[73250] => 1
[125313] => 2
[142062] => 1
[342520] => 2
[357301] => 2
[357303] => 1
[404419] => 1
[408957] => 1
[415891] => 2
[455894] => 1
[460119] => 1
[582332] => 1
[582333] => 1
[602886] => 1
)
My aim is to order them by the single digit value so the output would put the 2's(or highest number) to the top
Array
(
[1845267] => 2
[1845256] => 2
[1845260] => 2
[415891] => 2
[125313] => 2
[67376] => 2
[342520] => 2
[357301] => 2
[33636] => 1
[73250] => 1
[142062] => 1
[357303] => 1
[404419] => 1
[408957] => 1
[455894] => 1
[460119] => 1
[582332] => 1
[582333] => 1
[602886] => 1
)
Try with the arsort function:
arsort — Sort an array in reverse order and maintain index association
Example:
arsort($array);
// done, $array is now sorted
Built into PHP: http://php.net/manual/en/function.arsort.php
bascially you need toh known array function for php from here
and function as
arsort($arr);
Simple
arsort($array)
http://www.php.net/manual/en/function.arsort.php

Categories