Sorting Multidimensional array several levelsCustom sorting multidimensional with n items by highest valueCreate ArrayList from arrayHow do I check if an array includes an object in JavaScript?How to append something to an array?Deleting an element from an array in PHPSort array of objects by string property valueHow do I empty an array in JavaScript?Loop through an array in JavaScriptHow to check if an object is an array?How do I remove a particular element from an array in JavaScript?For-each over an array in JavaScript?

How can I use my cell phone's light as a reading light?

What is the relationship between external and internal composition in a cartesian closed category?

Why does Trump want a citizenship question on the census?

With a data transfer of 50 GB estimated 5 hours, are USB-C claimed speeds inaccurate or to blame?

Converting "type":"MultiLineString","coordinates":[[[x1,y1],[x2,y2]]] to geography column using PostGIS?

Deck of Cards with Shuffle and Sort functionality

How do ballistic trajectories work in a ring world?

Can Jimmy hang on his rope?

Is there a formal/better word than "skyrocket" for the given context?

Appropriate conduit for several data cables underground over 300' run

My previous employer committed a severe violation of the law and is also being sued by me. How do I explain the situation to future employers?

Is it possible to complete a PhD in CS in 3 years?

Wires do not connect in Circuitikz

Write a function

How do I talk to my wife about unrealistic expectations?

Other Space Shuttle O-ring failures

What are the consequences for a developed nation to not accept any refugees?

When do flights get cancelled due to fog?

This LM317 diagram doesn't make any sense to me

Strong Password Detection in Python

QR codes, do people use them?

Where are the Wazirs?

Blocks from @ jafe

Moving millions of files to a different directory with specfic name patterns



Sorting Multidimensional array several levels


Custom sorting multidimensional with n items by highest valueCreate ArrayList from arrayHow do I check if an array includes an object in JavaScript?How to append something to an array?Deleting an element from an array in PHPSort array of objects by string property valueHow do I empty an array in JavaScript?Loop through an array in JavaScriptHow to check if an object is an array?How do I remove a particular element from an array in JavaScript?For-each over an array in JavaScript?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








3















I am currently trying to sort a multidimensional array by its totalPoints. Each array lineupSet has an n amount of items. I am trying to get the lineupSet with the the highest total totalPoints. How could I achieve most efficiently? the below is sudocode and therefore not working. Unsure how to approach this.



Code



public function getHighestTotalPoints($testArray)

if (isset($testArray) && !empty($testArray))
uasort($testArray, function ($a, $b)
return $a['lineupSet']['formula']['totalPoints'] <=> $b['lineupSet']['formula']['totalPoints'] ;
);
return array_reverse($testArray);

return null;


$testArray = [[
"lineupSet" => [
[[
"formula" => [
"totalPoints" => 214.61,
],
"name" => "test1",
], [
"formula" => [
"totalPoints" => 201.17,
],
"name" => "test2",
]], [
"formula" => [
"totalPoints" => 5.01,
],
"name" => "test3",
]],
], [
"lineupSet" => [
[[
"formula" => [
"totalPoints" => 220.66,
],
"name" => "test1",
], [
"formula" => [
"totalPoints" => 214.76,
],
"name" => "test2",
]],
],
], [
"lineupSet" => [
[[
"formula" => [
"totalPoints" => 205.71,
],
"name" => "test1",
], [
"formula" => [
"totalPoints" => 204.43,
],
"name" => "test2",
]],
],
], [
"lineupSet" => [
[[
"formula" => [
"totalPoints" => 205.48,
],
"name" => "test1",
], [
"formula" => [
"totalPoints" => 203.51,
],
"name" => "test2",
]],
],
]];


Desired result:



[0] => Array
(
[lineupSet] => Array
(
[0] => Array
(
[0] => Array
(
[formula] => Array
(
[totalPoints] => 220.66
)

[name] => test1
)

[1] => Array
(
[formula] => Array
(
[totalPoints] => 214.76
)

[name] => test2
)

)

)

)









share|improve this question



















  • 1





    Is totalPoints the only element in the formula array?

    – Nick
    Mar 25 at 22:35











  • @Nick hi, great question. So at times there could be more elements then totalPoints in formula

    – MaryCoding
    Mar 25 at 22:39











  • I get a syntax error. One too many [ somewhere

    – Ibu
    Mar 25 at 22:39











  • @Ibu there's one too many ], at the end of the first lineupSet

    – Nick
    Mar 25 at 22:40











  • @Ibu sorry, made the correction

    – MaryCoding
    Mar 25 at 22:41

















3















I am currently trying to sort a multidimensional array by its totalPoints. Each array lineupSet has an n amount of items. I am trying to get the lineupSet with the the highest total totalPoints. How could I achieve most efficiently? the below is sudocode and therefore not working. Unsure how to approach this.



Code



public function getHighestTotalPoints($testArray)

if (isset($testArray) && !empty($testArray))
uasort($testArray, function ($a, $b)
return $a['lineupSet']['formula']['totalPoints'] <=> $b['lineupSet']['formula']['totalPoints'] ;
);
return array_reverse($testArray);

return null;


$testArray = [[
"lineupSet" => [
[[
"formula" => [
"totalPoints" => 214.61,
],
"name" => "test1",
], [
"formula" => [
"totalPoints" => 201.17,
],
"name" => "test2",
]], [
"formula" => [
"totalPoints" => 5.01,
],
"name" => "test3",
]],
], [
"lineupSet" => [
[[
"formula" => [
"totalPoints" => 220.66,
],
"name" => "test1",
], [
"formula" => [
"totalPoints" => 214.76,
],
"name" => "test2",
]],
],
], [
"lineupSet" => [
[[
"formula" => [
"totalPoints" => 205.71,
],
"name" => "test1",
], [
"formula" => [
"totalPoints" => 204.43,
],
"name" => "test2",
]],
],
], [
"lineupSet" => [
[[
"formula" => [
"totalPoints" => 205.48,
],
"name" => "test1",
], [
"formula" => [
"totalPoints" => 203.51,
],
"name" => "test2",
]],
],
]];


Desired result:



[0] => Array
(
[lineupSet] => Array
(
[0] => Array
(
[0] => Array
(
[formula] => Array
(
[totalPoints] => 220.66
)

[name] => test1
)

[1] => Array
(
[formula] => Array
(
[totalPoints] => 214.76
)

[name] => test2
)

)

)

)









share|improve this question



















  • 1





    Is totalPoints the only element in the formula array?

    – Nick
    Mar 25 at 22:35











  • @Nick hi, great question. So at times there could be more elements then totalPoints in formula

    – MaryCoding
    Mar 25 at 22:39











  • I get a syntax error. One too many [ somewhere

    – Ibu
    Mar 25 at 22:39











  • @Ibu there's one too many ], at the end of the first lineupSet

    – Nick
    Mar 25 at 22:40











  • @Ibu sorry, made the correction

    – MaryCoding
    Mar 25 at 22:41













3












3








3


1






I am currently trying to sort a multidimensional array by its totalPoints. Each array lineupSet has an n amount of items. I am trying to get the lineupSet with the the highest total totalPoints. How could I achieve most efficiently? the below is sudocode and therefore not working. Unsure how to approach this.



Code



public function getHighestTotalPoints($testArray)

if (isset($testArray) && !empty($testArray))
uasort($testArray, function ($a, $b)
return $a['lineupSet']['formula']['totalPoints'] <=> $b['lineupSet']['formula']['totalPoints'] ;
);
return array_reverse($testArray);

return null;


$testArray = [[
"lineupSet" => [
[[
"formula" => [
"totalPoints" => 214.61,
],
"name" => "test1",
], [
"formula" => [
"totalPoints" => 201.17,
],
"name" => "test2",
]], [
"formula" => [
"totalPoints" => 5.01,
],
"name" => "test3",
]],
], [
"lineupSet" => [
[[
"formula" => [
"totalPoints" => 220.66,
],
"name" => "test1",
], [
"formula" => [
"totalPoints" => 214.76,
],
"name" => "test2",
]],
],
], [
"lineupSet" => [
[[
"formula" => [
"totalPoints" => 205.71,
],
"name" => "test1",
], [
"formula" => [
"totalPoints" => 204.43,
],
"name" => "test2",
]],
],
], [
"lineupSet" => [
[[
"formula" => [
"totalPoints" => 205.48,
],
"name" => "test1",
], [
"formula" => [
"totalPoints" => 203.51,
],
"name" => "test2",
]],
],
]];


Desired result:



[0] => Array
(
[lineupSet] => Array
(
[0] => Array
(
[0] => Array
(
[formula] => Array
(
[totalPoints] => 220.66
)

[name] => test1
)

[1] => Array
(
[formula] => Array
(
[totalPoints] => 214.76
)

[name] => test2
)

)

)

)









share|improve this question
















I am currently trying to sort a multidimensional array by its totalPoints. Each array lineupSet has an n amount of items. I am trying to get the lineupSet with the the highest total totalPoints. How could I achieve most efficiently? the below is sudocode and therefore not working. Unsure how to approach this.



Code



public function getHighestTotalPoints($testArray)

if (isset($testArray) && !empty($testArray))
uasort($testArray, function ($a, $b)
return $a['lineupSet']['formula']['totalPoints'] <=> $b['lineupSet']['formula']['totalPoints'] ;
);
return array_reverse($testArray);

return null;


$testArray = [[
"lineupSet" => [
[[
"formula" => [
"totalPoints" => 214.61,
],
"name" => "test1",
], [
"formula" => [
"totalPoints" => 201.17,
],
"name" => "test2",
]], [
"formula" => [
"totalPoints" => 5.01,
],
"name" => "test3",
]],
], [
"lineupSet" => [
[[
"formula" => [
"totalPoints" => 220.66,
],
"name" => "test1",
], [
"formula" => [
"totalPoints" => 214.76,
],
"name" => "test2",
]],
],
], [
"lineupSet" => [
[[
"formula" => [
"totalPoints" => 205.71,
],
"name" => "test1",
], [
"formula" => [
"totalPoints" => 204.43,
],
"name" => "test2",
]],
],
], [
"lineupSet" => [
[[
"formula" => [
"totalPoints" => 205.48,
],
"name" => "test1",
], [
"formula" => [
"totalPoints" => 203.51,
],
"name" => "test2",
]],
],
]];


Desired result:



[0] => Array
(
[lineupSet] => Array
(
[0] => Array
(
[0] => Array
(
[formula] => Array
(
[totalPoints] => 220.66
)

[name] => test1
)

[1] => Array
(
[formula] => Array
(
[totalPoints] => 214.76
)

[name] => test2
)

)

)

)






php arrays






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 25 at 22:52









Nick

48.2k14 gold badges24 silver badges44 bronze badges




48.2k14 gold badges24 silver badges44 bronze badges










asked Mar 25 at 22:30









MaryCodingMaryCoding

2383 silver badges20 bronze badges




2383 silver badges20 bronze badges







  • 1





    Is totalPoints the only element in the formula array?

    – Nick
    Mar 25 at 22:35











  • @Nick hi, great question. So at times there could be more elements then totalPoints in formula

    – MaryCoding
    Mar 25 at 22:39











  • I get a syntax error. One too many [ somewhere

    – Ibu
    Mar 25 at 22:39











  • @Ibu there's one too many ], at the end of the first lineupSet

    – Nick
    Mar 25 at 22:40











  • @Ibu sorry, made the correction

    – MaryCoding
    Mar 25 at 22:41












  • 1





    Is totalPoints the only element in the formula array?

    – Nick
    Mar 25 at 22:35











  • @Nick hi, great question. So at times there could be more elements then totalPoints in formula

    – MaryCoding
    Mar 25 at 22:39











  • I get a syntax error. One too many [ somewhere

    – Ibu
    Mar 25 at 22:39











  • @Ibu there's one too many ], at the end of the first lineupSet

    – Nick
    Mar 25 at 22:40











  • @Ibu sorry, made the correction

    – MaryCoding
    Mar 25 at 22:41







1




1





Is totalPoints the only element in the formula array?

– Nick
Mar 25 at 22:35





Is totalPoints the only element in the formula array?

– Nick
Mar 25 at 22:35













@Nick hi, great question. So at times there could be more elements then totalPoints in formula

– MaryCoding
Mar 25 at 22:39





@Nick hi, great question. So at times there could be more elements then totalPoints in formula

– MaryCoding
Mar 25 at 22:39













I get a syntax error. One too many [ somewhere

– Ibu
Mar 25 at 22:39





I get a syntax error. One too many [ somewhere

– Ibu
Mar 25 at 22:39













@Ibu there's one too many ], at the end of the first lineupSet

– Nick
Mar 25 at 22:40





@Ibu there's one too many ], at the end of the first lineupSet

– Nick
Mar 25 at 22:40













@Ibu sorry, made the correction

– MaryCoding
Mar 25 at 22:41





@Ibu sorry, made the correction

– MaryCoding
Mar 25 at 22:41












1 Answer
1






active

oldest

votes


















1














You can use usort to sort the array according to a custom function. This function determines the totalPoints for a given lineupSet:



function sum_points($v) 
$totalPoints = 0;
foreach ($v['lineupSet'] as $lset)
if (isset($lset['formula']))
$totalPoints += $lset['formula']['totalPoints'];

else
foreach ($lset as $l)
$totalPoints += $l['formula']['totalPoints'];



return $totalPoints;



To sort descending (so the maximum total points is in the first entry of the array), we then use a sort function which returns a positive number when the second value's totalPoints are bigger than the first, a negative number when it's smaller and 0 when they are the same:



function sort_points($a, $b) 
return sum_points($b) - sum_points($a);



Finally we call usort with this function and output the first element of the array:



usort($testArray, 'sort_points');
print_r($testArray[0]);


Output:



Array (
[lineupSet] => Array
(
[0] => Array
(
[0] => Array
(
[formula] => Array
(
[totalPoints] => 220.66
)

[name] => test1
)

[1] => Array
(
[formula] => Array
(
[totalPoints] => 214.76
)

[name] => test2
)

)

)

)


Demo on 3v4l.org






share|improve this answer























  • could I please use your help here: stackoverflow.com/questions/55720393/…

    – MaryCoding
    Apr 17 at 12:19










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55347326%2fsorting-multidimensional-array-several-levels%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You can use usort to sort the array according to a custom function. This function determines the totalPoints for a given lineupSet:



function sum_points($v) 
$totalPoints = 0;
foreach ($v['lineupSet'] as $lset)
if (isset($lset['formula']))
$totalPoints += $lset['formula']['totalPoints'];

else
foreach ($lset as $l)
$totalPoints += $l['formula']['totalPoints'];



return $totalPoints;



To sort descending (so the maximum total points is in the first entry of the array), we then use a sort function which returns a positive number when the second value's totalPoints are bigger than the first, a negative number when it's smaller and 0 when they are the same:



function sort_points($a, $b) 
return sum_points($b) - sum_points($a);



Finally we call usort with this function and output the first element of the array:



usort($testArray, 'sort_points');
print_r($testArray[0]);


Output:



Array (
[lineupSet] => Array
(
[0] => Array
(
[0] => Array
(
[formula] => Array
(
[totalPoints] => 220.66
)

[name] => test1
)

[1] => Array
(
[formula] => Array
(
[totalPoints] => 214.76
)

[name] => test2
)

)

)

)


Demo on 3v4l.org






share|improve this answer























  • could I please use your help here: stackoverflow.com/questions/55720393/…

    – MaryCoding
    Apr 17 at 12:19















1














You can use usort to sort the array according to a custom function. This function determines the totalPoints for a given lineupSet:



function sum_points($v) 
$totalPoints = 0;
foreach ($v['lineupSet'] as $lset)
if (isset($lset['formula']))
$totalPoints += $lset['formula']['totalPoints'];

else
foreach ($lset as $l)
$totalPoints += $l['formula']['totalPoints'];



return $totalPoints;



To sort descending (so the maximum total points is in the first entry of the array), we then use a sort function which returns a positive number when the second value's totalPoints are bigger than the first, a negative number when it's smaller and 0 when they are the same:



function sort_points($a, $b) 
return sum_points($b) - sum_points($a);



Finally we call usort with this function and output the first element of the array:



usort($testArray, 'sort_points');
print_r($testArray[0]);


Output:



Array (
[lineupSet] => Array
(
[0] => Array
(
[0] => Array
(
[formula] => Array
(
[totalPoints] => 220.66
)

[name] => test1
)

[1] => Array
(
[formula] => Array
(
[totalPoints] => 214.76
)

[name] => test2
)

)

)

)


Demo on 3v4l.org






share|improve this answer























  • could I please use your help here: stackoverflow.com/questions/55720393/…

    – MaryCoding
    Apr 17 at 12:19













1












1








1







You can use usort to sort the array according to a custom function. This function determines the totalPoints for a given lineupSet:



function sum_points($v) 
$totalPoints = 0;
foreach ($v['lineupSet'] as $lset)
if (isset($lset['formula']))
$totalPoints += $lset['formula']['totalPoints'];

else
foreach ($lset as $l)
$totalPoints += $l['formula']['totalPoints'];



return $totalPoints;



To sort descending (so the maximum total points is in the first entry of the array), we then use a sort function which returns a positive number when the second value's totalPoints are bigger than the first, a negative number when it's smaller and 0 when they are the same:



function sort_points($a, $b) 
return sum_points($b) - sum_points($a);



Finally we call usort with this function and output the first element of the array:



usort($testArray, 'sort_points');
print_r($testArray[0]);


Output:



Array (
[lineupSet] => Array
(
[0] => Array
(
[0] => Array
(
[formula] => Array
(
[totalPoints] => 220.66
)

[name] => test1
)

[1] => Array
(
[formula] => Array
(
[totalPoints] => 214.76
)

[name] => test2
)

)

)

)


Demo on 3v4l.org






share|improve this answer













You can use usort to sort the array according to a custom function. This function determines the totalPoints for a given lineupSet:



function sum_points($v) 
$totalPoints = 0;
foreach ($v['lineupSet'] as $lset)
if (isset($lset['formula']))
$totalPoints += $lset['formula']['totalPoints'];

else
foreach ($lset as $l)
$totalPoints += $l['formula']['totalPoints'];



return $totalPoints;



To sort descending (so the maximum total points is in the first entry of the array), we then use a sort function which returns a positive number when the second value's totalPoints are bigger than the first, a negative number when it's smaller and 0 when they are the same:



function sort_points($a, $b) 
return sum_points($b) - sum_points($a);



Finally we call usort with this function and output the first element of the array:



usort($testArray, 'sort_points');
print_r($testArray[0]);


Output:



Array (
[lineupSet] => Array
(
[0] => Array
(
[0] => Array
(
[formula] => Array
(
[totalPoints] => 220.66
)

[name] => test1
)

[1] => Array
(
[formula] => Array
(
[totalPoints] => 214.76
)

[name] => test2
)

)

)

)


Demo on 3v4l.org







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 25 at 22:52









NickNick

48.2k14 gold badges24 silver badges44 bronze badges




48.2k14 gold badges24 silver badges44 bronze badges












  • could I please use your help here: stackoverflow.com/questions/55720393/…

    – MaryCoding
    Apr 17 at 12:19

















  • could I please use your help here: stackoverflow.com/questions/55720393/…

    – MaryCoding
    Apr 17 at 12:19
















could I please use your help here: stackoverflow.com/questions/55720393/…

– MaryCoding
Apr 17 at 12:19





could I please use your help here: stackoverflow.com/questions/55720393/…

– MaryCoding
Apr 17 at 12:19








Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55347326%2fsorting-multidimensional-array-several-levels%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript