1
Test scenario
I got the following array, where the records are related, ie the key of each level, is related to the other:
Array
(
[regs] => Array
(
[c1] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 2
[4] => 1
)
[c2] => Array
(
[0] => 10
[1] => 20
[2] => 30
[3] => 20
[4] => 10
)
[c3] => Array
(
[0] => 100
[1] => 200
[2] => 300
[3] => 200
[4] => 400
)
[c4] => Array
(
[0] => 01:00
[1] => 02:00
[2] => 03:00
[3] => 04:00
[4] => 05:00
)
)
)
Array: array ( 'regs' => array ( 'c1' => array ( 0 => '1', 1 => '2', 2 => '3', 3 => '2', 4 => '1', ), 'c2' => array ( 0 => '10', 1 => '20', 2 => '30', 3 => '20', 4 => '10', ), 'c3' => array ( 0 => '100', 1 => '200', 2 => '300', 3 => '200', 4 => '400', ), 'c4' => array ( 0 => '01:00', 1 => '02:00', 2 => '03:00', 3 => '04:00', 4 => '05:00', ), ), )
Goal
I would like to "order" the array but maintaining the relationship of values.
Example:
Ordering by c1, c2, c3
:
Array
(
[regs] => Array
(
[c1] => Array
(
[0] => 1
[1] => 1
[2] => 2
[3] => 2
[4] => 3
)
[c2] => Array
(
[0] => 10
[1] => 10
[2] => 20
[3] => 20
[4] => 30
)
[c3] => Array
(
[0] => 100
[1] => 400
[2] => 200
[3] => 200
[4] => 300
)
[c4] => Array
(
[0] => 01:00
[1] => 05:00
[2] => 02:00
[3] => 04:00
[4] => 03:00
)
)
)
Bonus
Break and group values according to conditions.
Example: unite all values you have c1, c2, c3
equal:
Array
(
[regs] => Array
(
[r1] => Array
(
[c1] => Array
(
[0] => 1
)
[c2] => Array
(
[0] => 10
)
[c3] => Array
(
[0] => 100
)
[c4] => Array
(
[0] => 01:00
)
)
[r2] => Array
(
[c1] => Array
(
[0] => 2
[1] => 2
)
[c2] => Array
(
[0] => 20
[1] => 20
)
[c3] => Array
(
[0] => 200
[1] => 200
)
[c4] => Array
(
[0] => 02:00
[1] => 04:00
)
)
[r3] => Array
(
[c1] => Array
(
[0] => 3
)
[c2] => Array
(
[0] => 30
)
[c3] => Array
(
[0] => 300
)
[c4] => Array
(
[0] => 03:00
)
)
[r4] => Array
(
[c1] => Array
(
[0] => 1
)
[c2] => Array
(
[0] => 10
)
[c3] => Array
(
[0] => 400
)
[c4] => Array
(
[0] => 05:00
)
)
)
)
- There are native functions that do this automatically?
- How could I make these "ordinations"?
Related: How to make a foreach for an array of arrays?
– Jorge B.
You only need to go inside to go sorted by the sublevels of your array, going to sort the innermost levels until you go to the outermost last, making ifs in the desired conditions for sorting. It is not complex but can be confusing, research on ordering arrays in sublevels.
– Sabrina