> For the complete documentation index, see [llms.txt](https://andrearufo.gitbook.io/notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://andrearufo.gitbook.io/notes/php/ordinare-un-array-multidimensionale-per-le-chiavi-del-sottoarray.md).

# Ordinare un array multidimensionale per le chiavi del sottoarray

Se un array ha al suo interno una serie di altri array, tutti uguali, con gli stessi sottoelementi e si vuole ordinare questo primo array secondo ordine degli elementi del sottoarray viene comoda la funzione `array_multisort` che permette di ordinare un array secondo l'ordine di un secondo array.

In pratica:

```php
<?php

$list = [
    'one' => [
        'positive' => 3,
        'negative' => 2
    ],
    'two' => [
        'positive' => 1,
        'negative' => 0
    ],
    'three' => [
        'positive' => 5,
        'negative' => 0
    ],
    'four' => [
        'positive' => 4,
        'negative' => 5
    ],
    'five' => [
        'positive' => 6,
        'negative' => 2
    ],
];

foreach ($list as $key => $value) {
    $list[$key]['score'] = $value['positive'] - $value['negative'];
}

foreach ($list as $key => $value) {
    $score[$key]    = $value['score'];
    $positive[$key] = $value['positive'];
    $negative[$key] = $value['negative'];
}

array_multisort(
    $score, SORT_DESC, 
    $positive, SORT_DESC, 
    $negative, SORT_ASC, 
    $list
);

echo '<pre>'.print_r($list, 1).'</pre>';
```

Partendo dalla lista faccio un primo ciclo per trovare lo `score` che è la differenza tra `positive - negative`. È il parametro per cui voglio ordinare in prima istanza: per chi ha più `score`, poi voglio ordinare per chi ha più `positive` e infine per chi ha meno `negative`.

Faccio quindi un secondo ciclo per avere una lista di ogni punteggio: `$score`; `$positive`; `$negative`. Questi nuovi array, dotati delle stesse chiavi di `$list` saranno i parametri di ordinamento di `$list` stesso attraverso la funzione `array_multisort` che prende poi come ultimo parametro l'array da ordinare secondo quei parametri.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://andrearufo.gitbook.io/notes/php/ordinare-un-array-multidimensionale-per-le-chiavi-del-sottoarray.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
