php - Foreach overwrites previous records in multidimensional array -


i have multidimensional array, , want show array in tabular form like

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

the following array i've

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

and on have tired below code don't know whats problem code.

foreach($rows $row) {     echo '<th>'.$row->label.'</th></tr>';     $i = 1;     $final = [];     foreach($row->value $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; 

the print_r output have provided not consistent -- has been manipulated add label keys. print_r have output them [label], not "label", also, print_r not have output comma @ end of same line.

so, risk solution not work, because have not provided correct array, provide here solution based on following array:

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

then solution this:

// collect unique list of "firstname" values $rowheaders = array(); foreach($finalarr $column) {     $rowheaders = array_merge($rowheaders, array_column($column["value"], "firstname")); } $rowheaders = array_unique($rowheaders); usort($rowheaders, 'strcasecmp');  // column headers $colheaders = array_column($finalarr, "label");  // create matrix: first column $matrix = array(array_merge(array("name"), $colheaders)); foreach($rowheaders $firstname) {     $matrix[] = array($firstname); } // ... , colour columns: foreach($finalarr $colindex => $column) {     $names = array_column($column["value"], "firstname");     foreach ($rowheaders $rowindex => $name) {         $matrix[$rowindex+1][] = in_array($name, $names) ? $column["value"][array_search($name, $names)]["count"] : 0;     } }  // convert matrix html table $html = ""; $td = "th"; foreach ($matrix $row) {     $html .= "<tr><$td>" . implode("</$td><$td>", $row) . "</$td></tr>\n";      $td = "td"; } $html = "<table border=1>\n$html</table>";  // output html: echo $html; 

the output is:

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

which in browser renders as:

table output


Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -