简体   繁体   中英

Loop through array grouping indexes by value within that array (PHP)

I wish to create a list of items from an array, grouped by a value within that array.

Take this array:

$people = array(
    0 => array(
         "Forename" => "Jim",
         "Surname"  => "Smith"
    ),
    1 => array(
         "Forename" => "Mike",
         "Surname"  => "Johnson"
    ),
    2 => array(
         "Forename" => "Kim",
         "Surname"  => "Smith"
    ),
    3 => array(
         "Forename" => "Paul",
         "Surname"  => "Jones"
    )
);

Specifically I'd like to run a foreach on $people , grouping them by unique surname. ie the desired output would be:

<select>
    <optgroup label="Smith">
        <option>Jim</option>
        <option>Kim</option>
    </optgroup>
    <optgroup label="Johnson">
        <option>Mike</option>
    </optgroup>
    <optgroup label="Jones">
        <option>Paul</option>
    </optgroup>
</select>

I'm struggling to come up with anything vaguely efficient and the Google gods aren't watching over me today :( What's the best approach for such a use-case in PHP?

$surnames = array();
foreach($people as $person) {
    $surnames[$person['surname']][] = $person;
}

This code stores all persons in an array grouped by their surnames.

The resulting array:

array(
 'smith' => array(
   0 => array(
     "Forename" => "Jim",
     "Surname"  => "Smith"
   ),
   1 => array(
     "Forename" => "Kim",
     "Surname"  => "Smith"
   )
 ),
 'jones' => array(
   0 => array(
     "Forename" => "Paul",
     "Surname"  => "Jones"
   )
 )
)

I would do this way:

$grouped = array();

foreach ($people as $p){
    if (!array_key_exist($p["Surname"], $grouped)){
        $grouped[$p["Surname"]] = array();
    }
    $grouped[$p["Surname"]][] = $p;
}

I've added one more duplicate person:

 ....
    4 => array
    (
        "Forename" => "Kim",
        "Surname"  => "Smith"
    )
);

this is how you filter array :

$uniqueNames = array();

foreach($people as $person)
{
    $uniqueNames[$person['Surname']][] = $person['Forename'];
}

if however you also need Forename to be unique, you can do it like this:

$uniqueNames = array_map
(
    function($arrayItem)
    {
        if (is_array($arrayItem))
        {
            return array_unique($arrayItem);
        }
    }
    , $uniqueNames
);

Also I've made some easy functions to generate html code:

function htmlSelect($name, $optionsData, $selectedItem = null)
{
    $str = "\n<select name='$name' id='select-$name'>";

    foreach ($optionsData as $k => $value_s)
    {
        if(is_array($value_s))
        {
            $str .= htmlOptgroup($k, $value_s);
        }
        else 
        {
            $selected = ($selectedItem && $selectedItem == $k);
            $str .= "\n\t".htmlOption($value_s, $k, $selected);
        }
    }

    $str .= "\n</select>";

    return $str;
}

function htmlOptgroup($label, $optionsData, $selectedItem = null)
{
    $str = "\n\t<optgroup label='$label'>";

    foreach ($optionsData as $k => $value)
    {
        $selected = ($selectedItem && $selectedItem == $k);
        $str .= "\n\t\t".htmlOption($value, $k, $selected);
    }

    $str .= "\n\t</optgroup>";

    return $str;
}

function htmlOption($display, $value, $selected = false)
{
    $selectedStr = $selected ? " selected='selected'" : "" ;

    return "<option$selectedStr value='$value'>$display</option>";
}

these functions can easily move to static class for html .

finally you call:

echo htmlSelect('unique-surnames', $uniqueNames);

I swear when I started there was no any answers :d

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM