-2

I have the following array and I want to sort the subarrays by value.

[
    'bwin' => [
        ['bookie' => 'bwin', 'id_bookie' => 178537, 'value' => 6.00, 'bet' => 1],
        ['bookie' => 'bwin', 'id_bookie' => 178537, 'value' => 1.45, 'bet' => 2],
        ['bookie' => 'bwin', 'id_bookie' => 178537, 'value' => 4.50, 'bet' => 'x']
    ],
    'NordicBet' => [
        ['bookie' => 'NordicBet', 'id_bookie' => 201581, 'value' => 5.75, 'bet' => 1],
        ['bookie' => 'NordicBet', 'id_bookie' => 201581, 'value' => 1.50, 'bet' => 2],
        ['bookie' => 'NordicBet', 'id_bookie' => 201581, 'value' => 4.30, 'bet' => 'x']
    ],
    'Canbet' => [
        ['bookie' => 'Canbet', 'id_bookie' => 176582, 'value' => 5.60, 'bet' => 1],
        ['bookie' => 'Canbet', 'id_bookie' => 176582, 'value' => 1.56, 'bet' => 2],
        ['bookie' => 'Canbet', 'id_bookie' => 176582, 'value' => 3.80, 'bet' => 'x']
    ],
    'Expekt' => [
        ['bookie' => 'Expekt', 'id_bookie' => 235615, 'value' => 5.60, 'bet' => 1],
        ['bookie' => 'Expekt', 'id_bookie' => 235615, 'value' => 1.50, 'bet' => 2],
        ['bookie' => 'Expekt', 'id_bookie' => 235615, 'value' => 4.25, 'bet' => 'x']
    ],
    'StanJames' => [
        ['bookie' => 'StanJames', 'id_bookie' => 243649, 'value' => 5.50, 'bet' => 1],
        ['bookie' => 'StanJames', 'id_bookie' => 243649, 'value' => 1.53, 'bet' => 2],
        ['bookie' => 'StanJames', 'id_bookie' => 243649, 'value' => 4.00, 'bet' => 'x']
    ],
    'Gamebookers' => [
        ['bookie' => 'Gamebookers', 'id_bookie' => 203620, 'value' => 5.00, 'bet' => 1],
        ['bookie' => 'Gamebookers', 'id_bookie' => 203620, 'value' => 1.50, 'bet' => 2],
        ['bookie' => 'Gamebookers', 'id_bookie' => 203620, 'value' => 4.25, 'bet' => 'x']
    ],
    'Tipp3' => [
        ['bookie' => 'Tipp3', 'id_bookie' => 292604, 'value' => 4.10, 'bet' => 1],
        ['bookie' => 'Tipp3', 'id_bookie' => 292604, 'value' => 1.50, 'bet' => 2],
        ['bookie' => 'Tipp3', 'id_bookie' => 292604, 'value' => 3.40, 'bet' => 'x']
    ]
];

This is the unsorted array output, but I want to sort the array where the line is 2, but the line will be changing, sometimes I have to sort where the line is x or 1.

enter image description here

4
  • @eykanal: I agree with mickmackusa regarding comments on the (now deleted) answer. Questions that are old can (and should) still attract answers and edits. This approach flows from the ethic that we're collectively building Q&A documentation, and such material is always in need of maintenance. Commented Jan 22, 2024 at 21:57
  • 1
    In relation to answers, the latest approach is that every answer post should be a fulsome response to the question, rather than commentary on the circumstances of the question author, or the pros/cons of the programming language. I appreciate that it is frustrating if an old answer is deleted based on guidelines that were to emerge later, but I think that is how things are run here, for the most part. Commented Jan 22, 2024 at 21:59
  • 1
    @eykanal Input data should be in a form that is easily ingested by PHP. Valid PHP code (var_export output) is preferred over print_r output. Commented Jan 22, 2024 at 22:47
  • @gre_gor: no objections from me if you want to roll back the edit. Commented Jan 26, 2024 at 18:32

2 Answers 2

0

Iterate over the sets and modify the rows by reference.
Call usort() to sort the subsets by the value column values.

Code: (Demo)

foreach ($array as &$row) {
    usort($row, fn($a, $b) => $b['value'] <=> $a['value']);
}
var_export($array);
Sign up to request clarification or add additional context in comments.

4 Comments

I think they actually want to sort the top array based on the subarray's value chosen by "bet" aka column name.
Well that has the same result. Sorting by bet to the custom priority of 1, then x, then 2 is the same as sorting by value. Is this what you mean? The top level keys (and their rows) do not change position at all between the sample input and desired output -- only the subarrays are sorted. I'll agree that the wording in the question isn't awesome. Shall this page simply be destroyed? @gre
Look at their image of the table (which is sorted by "1", as is their input data, despite their claim being unsorted). They want it sorted either by the column "1", "x" or "2". You are just sorting the columns themselves, instead of the rows.
Yes, I did notice that previously. That very well could be a symptom of the desired result. I cannot know for sure.
-1

You can use usort. Here's a small snippet. You can do pretty much want you want in this function.

function sorting_function($x, $y)
{
    if ($x['value'] == $y['value'])
        return 0;
    else if ($x['value'] < $y['value'])
        return -1;
    else
        return 1;
}

usort($table_array, 'sorting_function');

3 Comments

This answer does not work on the provided input array. Proof: 3v4l.org/4MreE
@mickmackusa It's even worse when you run it against v5.3.6, the latest version at the time this answer was posted.
Interesting. 3v4l.org/U6uPM#v5.3.6 I didn't expect that kind of breakage.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.