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

时间:2016-01-28 09:16:37

标签: php arrays multidimensional-array foreach array-push

我有一个多维数组,我希望以表格形式显示此数组,如

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

2 个答案:

答案 0 :(得分:0)

只需在第一个循环之外初始化$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;

答案 1 :(得分:0)

您提供的[label]输出不一致 - 已经操纵它以添加标签键。 print_r会将它们输出为"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>

输出结果为:

{{1}}

在浏览器中呈现为:

table output