Foreach 覆盖多维数组中以前的记录

Foreach overwrites previous records in multidimensional array

我有一个多维数组,我想以表格形式显示这个数组,例如

+------+-----+--------+-------+-------+
| Name | red | yellow | green | white |
+------+-----+--------+-------+-------+
| abc  |   8 |      2 |     4 |     0 |
| xyz  |   2 |      8 |     0 |     0 |
+------+-----+--------+-------+-------+

下面是我得到的数组

[0] => array(
        'label'=> 'red',
        [value] => Array
        (
                [0] => array
                (
                        [count] => 47
                        [firstname] => Syst

                )
                [1] => array
                (
                        [count] => 2
                        [firstname] => xyz
                )

                [2] => array
                (
                        [count] => 8
                        [firstname] => abc
                )
)
[1] => array(
        'label'=> 'yellow',
        [value] => Array
        (
                [0] => array
                (
                        [count] => 4
                        [firstname] => dim
                )
                [1] => array
                (
                        [count] => 2
                        [firstname] => abc
                )
                [2] => array
                (
                        [count] => 8
                        [firstname] => xyz
                )
        )

等等 我已经厌倦了下面的代码,但我不知道代码有什么问题。

foreach($rows as $row)
{
    echo '<th>'.$row->label.'</th></tr>';
    $i = 1;
    $final = [];
    foreach($row->value as $v){
        $temp = [];
        $temp[$v->firstname.' '.$v->lastname] = [];
        $output = findKey($final, $v->firstname.' '.$v->lastname);
        if($output){
            $temp[$v->firstname.' '.$v->lastname]['count'] = $v->count ;
            $temp[$v->firstname.' '.$v->lastname][$row->label.'_'.$row->color] =  $v->count ;
        }
        array_push($final,$temp);
    }
}
print_r($rows); //die;

只需在第一个循环之外初始化 $final 数组。如下所示..

    $final = [];

    foreach($rows as $row)
    {
        echo '<th>'.$row->label.'</th></tr>';
        $i = 1;    
        foreach($row->value as $v){
            $temp = [];
            $temp[$v->firstname.' '.$v->lastname] = [];
            $output = findKey($final, $v->firstname.' '.$v->lastname);
            if($output){
                $temp[$v->firstname.' '.$v->lastname]['count'] = $v->count ;
                $temp[$v->firstname.' '.$v->lastname][$row->label.'_'.$row->color] =  $v->count ;
            }
            array_push($final,$temp);
        }
    }
    print_r($rows); //die;

您提供的 print_r 输出不一致 -- 它已被操纵以添加标签键。 print_r 会将它们输出为 [label],而不是 "label",而且,print_r 不会在同一行的末尾输出逗号。

因此,由于您没有提供正确的数组,因此存在解决方案不起作用的风险,我在此提供基于以下数组的解决方案:

$finalArr = 
array(
    array(
        'label' => 'red',
        'value' => Array (
                array ("count" => 47, "firstname" => 'Syst'),
                array ("count" => 2, "firstname" => 'xyz'),
                array ("count" => 8, "firstname" => 'abc')
        )
    ),
    array(
        'label' => 'yellow',
        "value" => Array (
                array ("count" => 4, "firstname" => 'dim'),
                array ("count" => 2, "firstname" => 'abc'),
                array ("count" => 8, "firstname" => 'xyz')
        )
    )
);

那么解决方案可能是这样的:

// collect unique list of "firstname" values
$rowHeaders = array();
foreach($finalArr as $column) {
    $rowHeaders = array_merge($rowHeaders, array_column($column["value"], "firstname"));
}
$rowHeaders = array_unique($rowHeaders);
usort($rowHeaders, 'strcasecmp');

// get column headers
$colHeaders = array_column($finalArr, "label");

// create matrix: first column
$matrix = array(array_merge(array("Name"), $colHeaders));
foreach($rowHeaders as $firstname) {
    $matrix[] = array($firstname);
}
// ... and colour columns:
foreach($finalArr as $colIndex => $column) {
    $names = array_column($column["value"], "firstname");
    foreach ($rowHeaders as $rowIndex => $name) {
        $matrix[$rowIndex+1][] = in_array($name, $names) ? $column["value"][array_search($name, $names)]["count"] : 0;
    }
}

// convert matrix into HTML table
$html = "";
$td = "th";
foreach ($matrix as $row) {
    $html .= "<tr><$td>" . implode("</$td><$td>", $row) . "</$td></tr>\n"; 
    $td = "td";
}
$html = "<table border=1>\n$html</table>";

// output html:
echo $html;

输出为:

<table border=1>
<tr><th>Name</th><th>red</th><th>yellow</th></tr>
<tr><td>abc</td><td>8</td><td>2</td></tr>
<tr><td>dim</td><td>0</td><td>4</td></tr>
<tr><td>Syst</td><td>47</td><td>0</td></tr>
<tr><td>xyz</td><td>2</td><td>8</td></tr>
</table>

在浏览器中呈现为: