php - sorting by points and then alphabetically - php

I have an array:
Array (
[0] => Array
( [points] => 10
[id] => 58
[nazwa] => auser1 )
[1] => Array
( [points] => 15
[id] => 36
[nazwa] => cuser2 )
[2] => Array
( [points] => 15
[id] => 57
[nazwa] => buser3 )
[3] => Array
( [points] => 20
[id] => 56
[nazwa] => duser4 )
[4] => Array
( [points] => 20
[id] => 54
[nazwa] => euser5 ))
I would like to sort this array by points and then alphabetically by nazwa.
How can I do this?
I would like to create final points table for Russia Cup!

if you want to sort your multidimensional array in sequence first points then with name then you have to create your multidimensional array in same sequence format
example: first element should be points, second name, last id. Refer following sequence.
$array = [ [ 'points' => 10, 'nazwa' => 'auser1', 'id' => 58 ],
[ 'points' => 15, 'nazwa' => 'cuser2', 'id' => 36 ],
[ 'points' => 15, 'nazwa' => 'buser3', 'id' => 57 ],
[ 'points' => 20, 'nazwa' => 'duser4', 'id' => 56 ],
[ 'points' => 20, 'nazwa' => 'euser5', 'id' => 54 ]];
array_multisort( $array );
print_r(($array));
Output:
Array
(
[0] => Array
(
[points] => 10
[nazwa] => auser1
[id] => 58
)
[1] => Array
(
[points] => 15
[nazwa] => buser3
[id] => 57
)
[2] => Array
(
[points] => 15
[nazwa] => cuser2
[id] => 36
)
[3] => Array
(
[points] => 20
[nazwa] => duser4
[id] => 56
)
[4] => Array
(
[points] => 20
[nazwa] => euser5
[id] => 54
)
)

Related

PHP Subtract multidimensional arrays based on 2 values

I need to subtract the qt from two arrays based on id and type, keeping the array complete.
How do I do in php to be able to subtract these arrays?
I have 2 arrays:
=> this is the first array with multiple key "down"
Array
(
[0] => Array
(
[id] => 26
[loc] => 1
[type] => down
[qt] => 12
)
[1] => Array
(
[id] => 32
[loc] => 1
[type] => down
[qt] => 34
)
[2] => Array
(
[id] => 26
[loc] => 2
[type] => down
[qt] => 5
)
[3] => Array
(
[id] => 86
[loc] => 3
[type] => down
[qt] => 45
)
[4] => Array
(
[id] => 23
[loc] => 9
[type] => down
[qt] => 3
)
[5] => Array
(
[id] => 23
[loc] => 3
[type] => down
[qt] => 99
)
)
=> this is the second array with multiple key "up"
Array
(
[0] => Array
(
[id] => 26
[loc] => 1
[type] => up
[qt] => 5
)
[1] => Array
(
[id] => 86
[loc] => 3
[type] => up
[qt] => 27
)
[2] => Array
(
[id] => 23
[loc] => 9
[type] => up
[qt] => 3
)
)
=> I need cubtract "qt" (if "id" and "loc" are the same then subtract the "qt")
Array
(
[0] => Array
(
[id] => 26
[loc] => 1
[type] => total
[qt] => 7
)
[1] => Array
(
[id] => 32
[loc] => 1
[type] => total
[qt] => 34
)
[2] => Array
(
[id] => 26
[loc] => 2
[type] => total
[qt] => 5
)
[3] => Array
(
[id] => 86
[loc] => 3
[type] => total
[qt] => 18
)
[4] => Array
(
[id] => 23
[loc] => 9
[type] => total
[qt] => 0
)
[5] => Array
(
[id] => 23
[loc] => 3
[type] => down
[qt] => 99
)
)
Try this out
function findMatch($array, $id, $loc)
{
foreach ($array as $key => $item) {
if ($item["id"] == $id and $item["loc"] == $loc) {
return $key;
}
}
return false;
}
$total = [];
foreach($down as $d) {
$matched = findMatch($up, $d["id"], $d["loc"]);
if($matched !== false){
$total[] = array_replace($d, [
"type" => "total",
"qt" => ($d["qt"] - $up[$matched]["qt"])
]);
} else {
$total[] = array_replace($d, ["type" => "total"]);
}
}
print_r($total);

Convert Associative arrays into different format

I have a associative array like this
(
[0] => Array
(
[userId] => 4785
[courseId] => 1774
[EnterpriseId] => 13
[lbaseid] => 1697
[progress] => 100
[milestone] => 5
)
[1] => Array
(
[userId] => 4786
[courseId] => 1775
[EnterpriseId] => 13
[lbaseid] => 1698
[progress] => 100
[milestone] => 5
)
[2] => Array
(
[userId] => 4786
[courseId] => 1776
[EnterpriseId] => 13
[lbaseid] => 1699
[progress] => 100
[milestone] => 5
)
[3] => Array
(
[userId] => 4786
[courseId] => 1777
[EnterpriseId] => 13
[lbaseid] => 1700
[progress] => 100
[milestone] => 5
)
[4] => Array
(
[userId] => 4786
[courseId] => 1778
[EnterpriseId] => 13
[lbaseid] => 1701
[progress] => 100
[milestone] => 5
)
)
I want to convert this associative array like
(
4785_1774_13[0] => Array
(
[userId] => 4785
[courseId] => 1774
[EnterpriseId] => 13
[lbaseid] => 1697
[progress] => 100
[milestone] => 5
)
4786_1775_13[1] => Array
(
[userId] => 4786
[courseId] => 1775
[EnterpriseId] => 13
[lbaseid] => 1698
[progress] => 100
[milestone] => 5
)
4786_1776_13[2] => Array
(
[userId] => 4786
[courseId] => 1776
[EnterpriseId] => 13
[lbaseid] => 1699
[progress] => 100
[milestone] => 5
)
4786_1777_13[3] => Array
(
[userId] => 4786
[courseId] => 1777
[EnterpriseId] => 13
[lbaseid] => 1700
[progress] => 100
[milestone] => 5
)
4786_1778_13[4] => Array
(
[userId] => 4786
[courseId] => 1778
[EnterpriseId] => 13
[lbaseid] => 1701
[progress] => 100
[milestone] => 5
)
)
If you want to have a new array with keys constructed with the values from the original array, you can iterate through the initial array and construct a new array with the desired key format.
$array = [
[
"userId" => 11,
"courseId" => 22,
"EnterpriseId" => 33
],
[
"userId" => 44,
"courseId" => 55,
"EnterpriseId" => 66
]
];
var_dump($array);
$new_array = [];
foreach($array as $key => $value) {
$new_key = implode("_",[
$value["userId"],
$value["courseId"],
$value["EnterpriseId"]
]);
$new_array[$new_key] = $value;
}
var_dump($new_array);
Live example here: https://3v4l.org/1ibMl

pop and push from one associative array to another array

I want to pop from array2 and want to push in array1.
But according to some custom requirement.
Now in array1 there is 1st key's readingOrder is 1 and 2nd key's readingOrder is 4.
So i want to push between this two key from array2's first two key.And same process for all other.
and my final array must be like array3.
for example in array1 key[0] readingOrder is 1 and key[1]'s 4. Now i want to push another two key from array2.
for array1 key[2] reading order is 6. so before this key i want to push another one key from array2 and same for further...
array1 is like below
Array
(
[0] => Array
(
[readingOrder] => 1
[id] => 78
)
[1] => Array
(
[readingOrder] => 4
[id] => 76
)
[2] => Array
(
[readingOrder] => 6
[id] => 80
)
)
array2 is like below
Array
(
[0] => Array
(
[id] => 81
[readingOrder] => 2
)
[1] => Array
(
[id] => 82
[readingOrder] => 5
)
[2] => Array
(
[id] => 84
[readingOrder] => 7
)
[3] => Array
(
[id] => 85
[readingOrder] => 8
)
[4] => Array
(
[id] => 86
[readingOrder] => 9
)
[5] => Array
(
[id] => 87
[readingOrder] => 10
)
[6] => Array
(
[id] => 88
[readingOrder] => 11
)
)
Output array3:
Array
(
[0] => Array
(
[readingOrder] => 1
[id] => 78
)
[1] => Array
(
[id] => 81
[readingOrder] => 2
)
[2] => Array
(
[id] => 82
[readingOrder] => 5
)
[3] => Array
(
[readingOrder] => 4
[id] => 76
)
[4] => Array
(
[id] => 84
[readingOrder] => 7
)
[5] => Array
(
[readingOrder] => 6
[id] => 80
)
[6] => Array
(
[id] => 85
[readingOrder] => 8
)
[7] => Array
(
[id] => 86
[readingOrder] => 9
)
[8] => Array
(
[id] => 87
[readingOrder] => 10
)
[9] => Array
(
[id] => 88
[readingOrder] => 11
)
)
Thanks..
You can build your array like that:
$current = 1;
$arr3 = [];
while ( $arr1 && $arr2 ) {
if ( $arr1[0]['readingOrder'] > $current )
$arr3[] = array_shift($arr2);
else
$arr3[] = array_shift($arr1);
$current++;
}
$arr3 = array_merge($arr3, $arr1, $arr2);
print_r($arr3);
Note that this code is destructive for $arr1 and $arr2. If you want to preserve them, copy them before and use the copies instead.
You can do this with usort. First you need to merge the arrays:
$a1 = [
[
'readingOrder' => 1,
'id' => 78
],
[
'readingOrder' => 4,
'id' => 76
],
[
'readingOrder' => 6,
'id' => 80
]
];
$a2 = [
[
'readingOrder' => 2,
'id' => 81
],
[
'readingOrder' => 5,
'id' => 82
],
[
'readingOrder' => 7,
'id' => 84
],
[
'readingOrder' => 8,
'id' => 85
]
];
$a3 = array_merge($a1, $a2);
Then you need to use usort:
usort($a3, function($a,$b) {
if ($a['readingOrder'] == $b['readingOrder']) return 0;
return $a['readingOrder'] < $b['readingOrder'] ? -1 : 1;
});
In PHP 7 you can now use the spaceship operator, which would make the code more clean. Like so:
usort($a3, function($a,$b) {
return $a[0] <=> $b[0];
});
This will then return:
(
[0] => Array
(
[readingOrder] => 1
[id] => 78
)
[1] => Array
(
[readingOrder] => 2
[id] => 81
)
[2] => Array
(
[readingOrder] => 4
[id] => 76
)
[3] => Array
(
[readingOrder] => 5
[id] => 82
)
[4] => Array
(
[readingOrder] => 6
[id] => 80
)
[5] => Array
(
[readingOrder] => 7
[id] => 84
)
[6] => Array
(
[readingOrder] => 8
[id] => 85
)
)

how to make an all arrays (including associative array) to single level of array using php?

Hope you can help me with this. Because I'm trying to reorder them but i need first to make the be at a single level of array. From associative array to single array.
$MY_ASSOC_ARRAY
Array
(
[0] => Array
(
[MAIN_ID] => 1
[ORDER] => 1
[NAME] => Animal
[PARENT_ID] => 0
[childs] => Array
(
[0] => Array
(
[MAIN_ID] => 4
[ORDER] => 4
[NAME] => doggie
[PARENT_ID] => 1
[childs] => Array
(
[0] => Array
(
[MAIN_ID] => 18
[ORDER] => 18
[NAME] => hunting
[PARENT_ID] => 4
[childs] => Array
(
[0] => Array
(
[MAIN_ID] => 21
[ORDER] => 21
[NAME] => setter
[PARENT_ID] => 18
)
[1] => Array
(
[MAIN_ID] => 22
[ORDER] => 22
[NAME] => pointer
[PARENT_ID] => 18
)
)
)
[1] => Array
(
[MAIN_ID] => 19
[ORDER] => 19
[NAME] => companion
[PARENT_ID] => 4
)
)
)
)
)
)
Alright now the array should not be in that multi level (associative) array instead it will look like this:
Array
(
[0] => Array
(
[MAIN_ID] => 1
[ORDER] => 1
[NAME] => Animal
[PARENT_ID] => 0
)
[1] => Array
(
[MAIN_ID] => 4
[ORDER] => 4
[NAME] => doggie
[PARENT_ID] => 1
)
[2] => Array
(
[MAIN_ID] => 18
[ORDER] => 18
[NAME] => hunting
[PARENT_ID] => 4
)
[3] => Array
(
[MAIN_ID] => 21
[ORDER] => 21
[NAME] => setter
[PARENT_ID] => 18
)
[4] => Array
(
[MAIN_ID] => 22
[ORDER] => 22
[NAME] => pointer
[PARENT_ID] => 18
)
[5] => Array
(
[MAIN_ID] => 19
[ORDER] => 19
[NAME] => companion
[PARENT_ID] => 4
)
)
I'm no sure how will that be possible in the most effecient way without using too much memory that will affect the speed with the use of Php Codeigniter. Thanks!
[UPDATE # 1]
here are the code that I have tried but the order is different
foreach($tree as $key => $value) {
$single[] = $value;
}
And this is the output for this failed attemp...
Array
(
[0] => Array
(
[MAIN_ID] => 1
[ORDER] => 1
[NAME] => Animal
[PARENT_ID] => 0
)
[1] => Array
(
[MAIN_ID] => 4
[ORDER] => 4
[NAME] => doggie
[PARENT_ID] => 1
)
[2] => Array
(
[MAIN_ID] => 18
[ORDER] => 18
[NAME] => hunting
[PARENT_ID] => 4
)
[3] => Array
(
[MAIN_ID] => 19
[ORDER] => 19
[NAME] => companion
[PARENT_ID] => 4
)
[4] => Array
(
[MAIN_ID] => 21
[ORDER] => 21
[NAME] => setter
[PARENT_ID] => 18
)
[5] => Array
(
[MAIN_ID] => 22
[ORDER] => 22
[NAME] => pointer
[PARENT_ID] => 18
)
)
The [NAME] => companion should be at the last array not on 4th ([3] => Array)
UPDATE # 2:
Feel bad about the down votes... if this question or problem is not useful on your end
<?php
$array = Array(
0 => Array
(
'MAIN_ID' => 1,
'ORDER' => 1,
'NAME' => 'Animal',
'PARENT_ID' => 0,
'childs' => Array
(
0 => Array
(
'MAIN_ID' => 4,
'ORDER' => 4,
'NAME' => 'doggie',
'PARENT_ID' => 1,
'childs' => Array
(
0 => Array
(
'MAIN_ID' => 18,
'ORDER' => 18,
'NAME' => 'hunting',
'PARENT_ID' => 4,
'childs' => Array
(
0 => Array
(
'MAIN_ID' => 21,
'ORDER' => 21,
'NAME' => 'setter',
'PARENT_ID' => 18,
),
1 => Array
(
'MAIN_ID' => 22,
'ORDER' => 22,
'NAME' => 'pointer',
'PARENT_ID' => 18,
)
)
),
1 => Array
(
'MAIN_ID' => 19,
'ORDER' => 19,
'NAME' => 'companion',
'PARENT_ID' => 4,
)
)
)
)
)
);
$out = [];
$out = generateArray($array, $out);
print_r($out);
function generateArray($in, $out){
foreach($in as $value){
$childs = false;
if(isset($value['childs'])){
$childs = $value['childs'];
unset($value['childs']);
}
$out[] = $value;
if($childs)
$out = generateArray($childs, $out);
}
return $out;
}
?>

Sort function that sorts alphabetical and numbers

I have an array that looks like this:
Array ( [0] => Array ( [id] => 1103 [age] => 37 [gen] => C:3:2:5:1:4 ) [1] => Array ( [id] => 1104 [age] => 37 [gen] => A:3:1:4:1 ) [2] => Array ( [id] => 1105 [age] => 36 [gen] => A:3:2:3:2 ) [3] => Array ( [id] => 1106 [age] => 32 [gen] => B:2:5:1:2:2 ) [4] => Array ( [id] => 1107 [age] => 31 [gen] => C:3:4:4:5:3 [5] => Array ( [id] => 1104 [age] => 37 [gen] => A:3:1:4:1:6 ))
I want to sort the array by the gen key so the result will be like this:
A:3:1:4:1:6
A:3:2:3:2
B:2:5:1:2:2
C:3:2:5:1:4
C:3:4:4:5:3
I tried this code:
uasort($newArray, function($a, $b) {
return strnatcasecmp($a['ngen'], $b['ngen']);
});
But canĀ“t get the correct result. Any ideas?
This function will help you to sort the array, just pass the key by which the array is to be sort.
function build_sorter($key)
{
return function ($a, $b) use ($key) {
return strnatcmp($a[$key], $b[$key]);
};
}
Example Code:
$results = array(0 => array ( 'id' => 1103, 'age' => 37, 'gen' => 'C:3:2:5:1:4' ),
1 => array ( 'id' => 1104, 'age' => 37, 'gen' => 'A:3:1:4:1' ),
2 => array ( 'id' => 1105, 'age' => 36, 'gen' => 'A:3:2:3:2' ),
3 => array ( 'id' => 1106, 'age' => 32, 'gen' => 'B:2:5:1:2:2' ),
4 => array ( 'id' => 1107, 'age' => 31, 'gen' => 'C:3:4:4:5:3' ),
5 => array ( 'id' => 1104, 'age' => 37, 'gen' => 'A:3:1:4:1:6' )
);
usort($results, build_sorter('gen'));
echo "<pre>"; print_r($results) ;
Just call build_sorter('gen') in usort callback and pass key gen
This will hive you :
Array
(
[0] => Array
(
[id] => 1104
[age] => 37
[gen] => A:3:1:4:1
)
[1] => Array
(
[id] => 1104
[age] => 37
[gen] => A:3:1:4:1:6
)
[2] => Array
(
[id] => 1105
[age] => 36
[gen] => A:3:2:3:2
)
[3] => Array
(
[id] => 1106
[age] => 32
[gen] => B:2:5:1:2:2
)
[4] => Array
(
[id] => 1103
[age] => 37
[gen] => C:3:2:5:1:4
)
[5] => Array
(
[id] => 1107
[age] => 31
[gen] => C:3:4:4:5:3
)
)
<?php
$ar = [
["id" => 1103, "age" => 37, "gen" => "C:3:2:5:1:4"],
["id" => 1104, "age" => 37, "gen" => "A:3:1:4:1"],
["id" => 1105, "age" => 36, "gen" => "A:3:2:3:2"],
["id" => 1106, "age" => 32, "gen" => "B:2:5:1:2:2"],
["id" => 1107, "age" => 31, "gen" => "C:3:4:4:5:3"],
["id" => 1108, "age" => 37, "gen" => "A:3:1:4:1:6"],
];
usort($ar, function ($a, $b) {
return strcmp($a['gen'], $b['gen']);
});
foreach($ar as $r)
echo($r['id'] . " " . $r['gen'] . PHP_EOL);
Will give you:
1104 A:3:1:4:1
1108 A:3:1:4:1:6
1105 A:3:2:3:2
1106 B:2:5:1:2:2
1103 C:3:2:5:1:4
1107 C:3:4:4:5:3

Categories