根据每个数组中的单个值对PHP数组元素进行分组
我目前有一个数组,看起来像:根据每个数组中的单个值对PHP数组元素进行分组,php,arrays,grouping,Php,Arrays,Grouping,我目前有一个数组,看起来像: Array ( [0] => Array ( [id] => 1 [name] => Test 1 [age] => 42 [another_id] => 5 ) [1] => Array ( [id] => 2
Array
(
[0] => Array
(
[id] => 1
[name] => Test 1
[age] => 42
[another_id] => 5
)
[1] => Array
(
[id] => 2
[name] => Test 2
[age] => 47
[another_id] => 3
)
[2] => Array
(
[id] => 3
[name] => Test 3
[age] => 30
[another_id] => 2
)
[3] => Array
(
[id] => 4
[name] => Test 7
[age] => 60
[another_id] => 3
)
[4] => Array
(
[id] => 5
[name] => Test 10
[age] => 38
[another_id] => 3
)
)
我希望做的是获取所有数组项并打印出项的报告,但按“另一个id”字段对它们进行分组。
例如,我期待的结果是:
REPORT
**Another id (2)**
Name: Test 3
**Another id (3)**
Name: Test 2
Name: Test 7
Name: Test 10
**Another id (5)**
Name: Test 1
我可以将项目组合在一起,但它们都保留在一个数组中,而且我似乎无法将它们彼此分离以生成报告
$grouped_types = array();
foreach($invalid_results as $type){
$grouped_types[$type['another_id']][] = $type;
}
print_r($grouped_types);
有人能帮我吗?非常感谢 我相信你想做的是:
$grouped_types = array();
foreach($invalid_results as $type){
$grouped_types[$type['another_id']][] = $type['name'];
}
var_dump($grouped_types);
输出:
array (size=3)
5 =>
array (size=1)
0 => string 'Test 1' (length=6)
3 =>
array (size=3)
0 => string 'Test 2' (length=6)
1 => string 'Test 7' (length=6)
2 => string 'Test 10' (length=7)
2 =>
array (size=1)
0 => string 'Test 3' (length=6)
首先创建一个数组以按另一个id分组
$grouped_types = array();
foreach($invalid_results as $type){
$grouped_types[$type['another_id']]['name'][] = $type['name'];
}
ksort($grouped_types); // sort by another id
显示为报告
foreach($grouped_types as $key=>$value){
echo "Another id ".$key."\n";
foreach($value['name'] as $k=>$v){
echo "Name: ".$v."\n";
}
echo "\n";
}
输出:
Another id 2
Name: Test 3
Another id 3
Name: Test 2
Name: Test 7
Name: Test 10
Another id 5
Name: Test 1
使用此代码可以添加任意数量的字段并显示它们
$grouped_types = array();
foreach($invalid_results as $type){
// add any number of fields here
$grouped_types[$type['another_id']][] = array('name'=>$type['name'],'age'=>$type['age']);
}
ksort($grouped_types);
print_r($grouped_types);
foreach($grouped_types as $key=>$value){
echo "Another ".$key."\n";
foreach($value as $k=>$v){
foreach($v as $g=>$r){
echo $g." ".$r.",";
}
echo "\n";
}
echo "\n";
}
实际上,您的分组逻辑非常接近。如果尚未为当前的“另一个id”创建分组,则只需添加一个零件即可。然后,输出实际上就像一对嵌套循环一样简单,遍历每个分组,然后遍历组中的每个项目
$grouped_types = array();
foreach($invalid_results as $type){
if(isset($grouped_types[$type['another_id']]))
{
$grouped_types[$type['another_id']][] = $type;
}
else
{
$grouped_types[$type['another_id']] = array($type);
}
}
foreach($grouped_types as $key => $array)
{
echo "**Another id (" . $key . ")**<br/>"; // if you're not outputting to HTML, use \n instead of <br/>
foreach($array as $type)
{
echo "Name: " . $type["name"] . "<br/>";// if you're not outputting to HTML, use \n instead of <br/>
}
echo "<br/>";
}
$grouped_types=array();
foreach($type作为$u结果无效){
如果(isset($grouped_types[$type['Other_id']]))
{
$grouped_types[$type['other_id']][]=$type;
}
其他的
{
$grouped_types[$type['other_id']]=数组($type);
}
}
foreach($key=>$array)
{
echo“**另一个id(“.$key”)***
”;//如果您没有输出到HTML,请使用\n而不是
foreach($数组作为$类型)
{
echo“Name:”.$type[“Name”]。“
”;//如果没有输出到HTML,请使用\n而不是
}
回声“
”;
}
您得到了什么输出/错误?@PaulStrobach所有数组元素都在那里,但只是按另一个id排序,我需要报告的“另一个id”字段有一个标题,该标题下面有与该id关联的每个名称。不幸的是,我似乎无法按“另一个id”分隔数组以形成报告!也许你需要检查
$grouped\u types[…]
是否不存在,然后创建它?如果你想要格式化输出,你需要循环$grouped\u types
,而不仅仅是转储它。否:他想保留原始数组的每个值,这似乎工作得很好!只是一个简单的问题,如果我要向数组中添加另一个字段(并尝试在屏幕上显示),我将如何操作?例如,一行原本会说“Name:Test1”的话会变成“Name:Test1,Age:30”来更新我的答案。