[英]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. 具体来说,我想在
$people
上运行foreach
,并按唯一的姓氏对它们进行分组。 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? 我正在努力想出任何效率不高的解决方案,而Google众神今天也没有注意我:(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: 但是,如果您还需要唯一的
Forename
,则可以这样:
$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: 另外,我已经做了一些简单的函数来生成
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>";
}
these functions can easily move to static class for html
. 这些函数可以轻松地移到
html
静态类。
finally you call: 最后你打电话给:
echo htmlSelect('unique-surnames', $uniqueNames);
I swear when I started there was no any answers :d 我发誓起步时没有任何回答:d
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.