Home > Mobile >  Sort associative arrays nested within other nested associative arrays (3 levels) in php
Sort associative arrays nested within other nested associative arrays (3 levels) in php

Time:03-31

I'm aiming to alphabetically order by name the last nesting level.

I've tried all different kinds of sort functions sort(), asort(), ksort()... , but I didn't end up with the needed results.

The array

$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' => 'Aguinaga, Karina', 'ext' => '(289) 5816'],        
        ['name' => 'Almanza, Janet', 'ext' => 'None None'],
        ['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' => 'Hannaford, Nora', 'ext' => '(429) 4597'],
        ['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'],
    ],
];

My attempts

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:

$result = array_map(function ($items) {
  $names = array_column($items, 'name');
  array_multisort($names, $items);
  return $items;
}, $array);

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);

Demo

CodePudding user response:

I was able to get it working, finally, by use what was suggested earlier, but, I must have had a type, because it now works.

I read my dynamically created array and used a foreach to read each line. Then I did a usort to compare each line in each language array:

foreach ($lang_txt_array1 as $keys => $langs) {
    usort($langs, 'cmp');
    $lang_txt_array1[$keys] = $langs;
}

function cmp($a, $b)
{
    return strcmp($a["name"], $b["name"]);
}

Thanks for all the help, and if I ask again, I will try to be better at getting my question across.

  • Related