Home > Mobile >  How to write a function to sort array office number?
How to write a function to sort array office number?

Time:08-09

how to sort array only by office number?

 $Array = [
[
    'Company' => 'Lotos',
    'offic' => [
        N40 => '4000$', 
        N32 => '1000$',
    ]
],
[
    'Company' => 'Lump',
    'offic' => [
        N32 => '7000$',
        N1 =>  '3400$',
        N40 => '1000$',
    ]
]

];

for example var_dump($result)

[N40]=>
   array {
["Lotos"]=>
string "6000$"
["Lump"]=>
string "1000$"

  [N1]=>
 array {
  ["Lump"]=>
  string "3400$"

i can't implement this method, i try to use function "uksort", "sort"...

 foreach ($Array as $key => $item){
 $data = uksort($item['offic']);

unsuccessfully

CodePudding user response:

Maybe this one helps

$newArray = [];
foreach ($Array as $value) {
    foreach ($value['offic'] as $officKey => $officValue) {
        $newArray[$officKey][] = [$value['Company'] => $officValue];
    }
}
  •  Tags:  
  • php
  • Related