I have created a multidimensional array and I am trying to sort part of the array, but everything I have tried has not worked that is listed in Stackoverflow.
I've tried all different kinds of sort function (sort()
, asort()
, ksort()
,...), but I didn't end up with the correct result.
I'm aiming for the a alphabetically order of the name in the last part of the array, 3rd dimension?
Here is what my array looks like:
Array (
[Dutch] => Array
(
[0] => Array
(
[name] => Kiburg, Natalie
[ext] => None 5977
)
)
[French] => Array
(
[49] => Array
(
[name] => Clausen, Katie
[ext] => (608) 5205
)
[50] => Array
(
[name] => Viglucci, Maria Tina
[ext] => (429) 6713
)
)
[German] => Array
(
[51] => Array
(
[name] => Pries, Nelli
[ext] => (289) 5846
)
[52] => Array
(
[name] => Calhamer, Tatiana
[ext] => (289) 5838
)
)
[Indonesian] => Array
(
[53] => Array
(
[name] => Meyer, Alexandra
[ext] => (931) 2094
)
)
[Korean] => Array
(
[47] => Array
(
[name] => Yoon, Matthew
[ext] => None 5990
)
)
[Laotian] => Array
(
[54] => Array
(
[name] => Inthavong, Andrew
[ext] => None 4695
)
)
[Lithuanian] => Array
(
[48] => Array
(
[name] => Paulius, Vilija
[ext] => (289) 5817
)
)
[Polish] => Array
(
[55] => Array
(
[name] => Arciuch, Eva
[ext] => (608) 5024
)
)
[Russian] => Array
(
[56] => Array
(
[name] => Blair, Catherine
[ext] => (608) 5206
)
)
[Sign Language] => Array
(
[57] => Array
(
[name] => Gough, Elizabeth
[ext] => (429) 4690
)
)
[Spanish] => Array
(
[1] => Array
(
[name] => Aguinaga, Karina
[ext] => (289) 5816
)
[2] => Array
(
[name] => Almanza, Janet
[ext] => None None
)
[3] => Array
(
[name] => Birkholz, Tina
[ext] => (289) 5812
)
[4] => Array
(
[name] => Bribiescas, Marisol
[ext] => (429) 4680
)
[5] => Array
(
[name] => Brotsos, Alexander
[ext] => (289) 5843
)
[6] => Array
(
[name] => Carapia, Jessica
[ext] => (429) 4673
)
[7] => Array
(
[name] => Chacon, Nohora
[ext] => (429) 4673
)
[8] => Array
(
[name] => Chang, Fernando
[ext] => (429) 4599
)
[9] => Array
(
[name] => Chavez, Flor
[ext] => (429) 6791
)
[10] => Array
(
[name] => Corona, Christina
[ext] => (429) 4685
)
[11] => Array
(
[name] => De La Fuente, Margaret
[ext] => (429) 4695
)
[12] => Array
(
[name] => Escalante, Adrian
[ext] => (742) 4690
)
[13] => Array
(
[name] => Espinoza, Laura
[ext] => (289) 5806
)
[14] => Array
(
[name] => Flores, Angel
[ext] => (289) 5837
)
[15] => Array
(
[name] => Fulgencio, Lucero
[ext] => (429) 5976
)
[16] => Array
(
[name] => Fuller, Jennifer
[ext] => (289) 5821
)
[17] => Array
(
[name] => Gordon, Terrence
[ext] => (289) 5800
)
[18] => Array
(
[name] => Gorostieta Reyes, Kathia
[ext] => None none
)
[19] => Array
(
[name] => Guadarrama, Erendira
[ext] => (608) 5223
)
[20] => Array
(
[name] => Hannaford, Nora
[ext] => (429) 4597
)
[21] => Array
(
[name] => Jaramillo, Erika
[ext] => None None
)
[22] => Array
(
[name] => Kampf, Allison
[ext] => (429) 5988
)
[23] => Array
(
[name] => Leon, Grisel
[ext] => (289) 5815
)
[24] => Array
(
[name] => Lopez, Isabel
[ext] => (429) 4597
)
[25] => Array
(
[name] => Mattos Cruz, Ariana
[ext] => None 4685
)
[26] => Array
(
[name] => McDonald, Stephanie
[ext] => None 5830
)
[27] => Array
(
[name] => Moore, Andrew
[ext] => (695) 7577
)
[28] => Array
(
[name] => Muro, Jasmine
[ext] => None 0000
)
[29] => Array
(
[name] => Muro, Stephanie
[ext] => (429) 6714
)
[30] => Array
(
[name] => Ocampo, Maria
[ext] => (429) 4682
)
[31] => Array
(
[name] => Padilla, Christine
[ext] => (289) 5829
)
[32] => Array
(
[name] => Ramirez, Abigail
[ext] => (429) 4680
)
[33] => Array
(
[name] => Raya, Adriana
[ext] => None 4685
)
[34] => Array
(
[name] => Rodriguez, Jazmin
[ext] => None none
)
[35] => Array
(
[name] => Ruiz, Maria
[ext] => None 1
)
[36] => Array
(
[name] => Saldivar, Marcos
[ext] => (695) 7577
)
[37] => Array
(
[name] => Serrato, Xitlaly
[ext] => (429) 4695
)
[38] => Array
(
[name] => Shroder, Elyssa
[ext] => (429) 4695
)
[39] => Array
(
[name] => Swaine, Mary
[ext] => (429) 5588
)
[40] => Array
(
[name] => Thompson, Susana
[ext] => (429) 6791
)
[41] => Array
(
[name] => Vargas, Meleny
[ext] => (429) 4690
)
[42] => Array
(
[name] => Vargas, Natalia
[ext] => None none
)
[43] => Array
(
[name] => Vargas, Unrico
[ext] => None none
)
[50] => Array
(
[name] => Viglucci, Maria Tina
[ext] => (429) 6713
)
[52] => Array
(
[name] => Calhamer, Tatiana
[ext] => (289) 5838
)
[53] => Array
(
[name] => Meyer, Alexandra
[ext] => (931) 2094
)
)
)
ksort($lang_txt_array1);
array_multisort($lang_txt_array1[0][1][2]);
uasort($lang_txt_array1, function($a, $b) { return strcmp($a[0], $b['name']); });
function sortByAge($a, $b) { return $a['name'] > $b['name']; }
usort($lang_txt_array1, 'sortByAge'); //$people is now sorted by age (ascending)
sort($lang_txt_array1, ksort($lang_txt_array1))
function cmp_sort($x,$y){ //your function to compare two keys
if($x===$y)
return 0;
else
return ($x<$y?1:-1);
}
uasort($lang_txt_array1,'cmp_sort'); //call user-defined compare function*/
function method1($a,$b) { return ($a[2]["name"]["dept"] <= $b[2]["name"]["dept"]) ? -1 : 1; } usort($array, "method1");`
CodePudding user response:
$lang_txt_array1 = [
'Dutch' => [
['name' => 'Kiburg, Natalie', 'ext' => 'None 5977'],
],
'French' => [
['name' => 'Clausen, Katie', 'ext' => '(608) 5205'],
['name' => 'Viglucci, Maria Tina', 'ext' => '(429) 6713'],
],
'German' => [
['name' => 'Pries, Nelli', 'ext' => '(289) 5846'],
['name' => 'Calhamer, Tatiana', 'ext' => '(289) 5838'],
],
'Indonesian' => [
['name' => 'Meyer, Alexandra', 'ext' => '(931) 2094'],
],
'Korean' => [
['name' => 'Yoon, Matthew', 'ext' => 'None 5990'],
],
'Laotian' => [
['name' => 'Inthavong, Andrew', 'ext' => 'None 4695'],
],
'Lithuanian' => [
['name' => 'Paulius, Vilija', 'ext' => '(289) 5817'],
],
'Polish' => [
['name' => 'Arciuch, Eva', 'ext' => '(608) 5024'],
],
'Russian' => [
['name' => 'Blair, Catherine', 'ext' => '(608) 5206'],
],
'Sign Language' => [
['name' => 'Gough, Elizabeth', 'ext' => '(429) 4690'],
],
'Spanish' => [
['name' => 'Hannaford, Nora', 'ext' => '(429) 4597'], // in the middle
['name' => 'Almanza, Janet', 'ext' => 'None None'], // second
['name' => 'Aguinaga, Karina', 'ext' => '(289) 5816'], // first
['name' => 'Birkholz, Tina', 'ext' => '(289) 5812'],
['name' => 'Bribiescas, Marisol', 'ext' => '(429) 4680'],
['name' => 'Brotsos, Alexander', 'ext' => '(289) 5843'],
['name' => 'Carapia, Jessica', 'ext' => '(429) 4673'],
['name' => 'Chacon, Nohora', 'ext' => '(429) 4673'],
['name' => 'Chang, Fernando', 'ext' => '(429) 4599'],
['name' => 'Chavez, Flor', 'ext' => '(429) 6791'],
['name' => 'Corona, Christina', 'ext' => '(429) 4685'],
['name' => 'De La Fuente, Margaret', 'ext' => '(429) 4695'],
['name' => 'Escalante, Adrian', 'ext' => '(742) 4690'],
['name' => 'Espinoza, Laura', 'ext' => '(289) 5806'],
['name' => 'Flores, Angel', 'ext' => '(289) 5837'],
['name' => 'Fulgencio, Lucero', 'ext' => '(429) 5976'],
['name' => 'Fuller, Jennifer', 'ext' => '(289) 5821'],
['name' => 'Gordon, Terrence', 'ext' => '(289) 5800'],
['name' => 'Gorostieta Reyes, Kathia', 'ext' => 'None none'],
['name' => 'Guadarrama, Erendira', 'ext' => '(608) 5223'],
['name' => 'Jaramillo, Erika', 'ext' => 'None None'],
['name' => 'Kampf, Allison', 'ext' => '(429) 5988'],
['name' => 'Leon, Grisel', 'ext' => '(289) 5815'],
['name' => 'Lopez, Isabel', 'ext' => '(429) 4597'],
['name' => 'Mattos Cruz, Ariana', 'ext' => 'None 4685'],
['name' => 'McDonald, Stephanie', 'ext' => 'None 5830'],
['name' => 'Moore, Andrew', 'ext' => '(695) 7577'],
['name' => 'Muro, Jasmine', 'ext' => 'None 0000'],
['name' => 'Muro, Stephanie', 'ext' => '(429) 6714'],
['name' => 'Ocampo, Maria', 'ext' => '(429) 4682'],
['name' => 'Padilla, Christine', 'ext' => '(289) 5829'],
['name' => 'Ramirez, Abigail', 'ext' => '(429) 4680'],
['name' => 'Raya, Adriana', 'ext' => 'None 4685'],
['name' => 'Rodriguez, Jazmin', 'ext' => 'None none'],
['name' => 'Ruiz, Maria', 'ext' => 'None 1'],
['name' => 'Saldivar, Marcos', 'ext' => '(695) 7577'],
['name' => 'Serrato, Xitlaly', 'ext' => '(429) 4695'],
['name' => 'Shroder, Elyssa', 'ext' => '(429) 4695'],
['name' => 'Swaine, Mary', 'ext' => '(429) 5588'],
['name' => 'Thompson, Susana', 'ext' => '(429) 6791'],
['name' => 'Vargas, Meleny', 'ext' => '(429) 4690'],
['name' => 'Vargas, Natalia', 'ext' => 'None none'],
['name' => 'Vargas, Unrico', 'ext' => 'None none'],
['name' => 'Viglucci, Maria Tina', 'ext' => '(429) 6713'],
['name' => 'Calhamer, Tatiana', 'ext' => '(289) 5838'],
['name' => 'Meyer, Alexandra', 'ext' => '(931) 2094'],
],
];
// Sorting the people within the languages
foreach($lang_txt_array1 as $lang => $arr){
usort($arr, fn($a, $b) => strcmp($a['name'], $b['name']));
$lang_txt_array1[$lang] = $arr;
}
// Sorting languages according to their first person
uasort($lang_txt_array1, fn($a, $b) => strcmp($a[0]['name'], $b[0]['name']));
print_r($lang_txt_array1);
CodePudding user response:
$result = array_map(function ($items) {
$names = array_column($items, 'name');
array_multisort($names, $items);
return $items;
}, $array);