在该数组中按值循环数组分组索引(PHP)

时间:2014-08-05 17:59:25

标签: php arrays

我希望从数组中创建一个项目列表,按该数组中的值进行分组。

拿这个数组:

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

具体来说,我想在foreach上运行$people,并按唯一姓氏对其进行分组。即所需的输出是:

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

我努力想出任何模糊效率的东西,Google今天没有关注我:(在PHP中使用这种用例的最佳方法是什么?

3 个答案:

答案 0 :(得分:1)

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

此代码将所有人员存储在按姓氏分组的数组中。

结果数组:

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

答案 1 :(得分:1)

我会这样做:

$grouped = array();

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

答案 2 :(得分:0)

我添加了一个重复的人:

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

这是你过滤数组的方法:

$uniqueNames = array();

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

如果你还需要Forename是唯一的,你可以这样做:

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

此外,我已经制作了一些简单的函数来生成html代码:

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>";
}

这些函数可以轻松移动到html的静态类。

最后你打电话:

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

我发誓当我开始时没有任何答案:d