0
 Actual i get

Array (
    [person1] => Array
        (
            [stage1] => 4
            [stage2] => 4
            [stage3] => 4
            [stage4] => 4
            [stage5] => 2
        )

    [person2] => Array
        (
            [stage3] => 1
        )

    [person3] => Array
        (
            [stage1] => 2
            [stage2] => 1
            [stage4] => 1
            [stage5] => 2
        )

) 

1)Here They are 5 stages stage1,stage2...stage5

2)they are 3 users person1 person2 person3

some person dont have stages please keep as zero or empty in that stage

Covert this array into table as below shown image please help me

https://i.stack.imgur.com/ccJLg.png

mks
  • 3
  • 4
  • It doesn't clear what you have and what you trying to achieve. Please make proper code formatting and give clear example what you have and then what should be achieved. – krylov123 Aug 23 '20 at 15:20
  • 1
    possible [duplicate](https://stackoverflow.com/questions/4746079/how-to-create-a-html-table-from-a-php-array/47068295#47068295) - please read [ask] – jibsteroos Aug 23 '20 at 15:21
  • get the max columns of a single item, then loop over it use the key to match the others if its not set, set to 0, (or you know the stages already simply loop over them) trivial.. seems like homework, SO is not for that please show what you have tried. – Lawrence Cherone Aug 23 '20 at 15:59
  • User '.$row['stage'].'';//here we get table header user,stage1,stage2...stageN as shown in image }?> foreach($originalArray as $key=>$t){ //person1,person2,person3 foreach( $t as $skey=>$p){ //stages should be bind here echo $skey; echo $p; echo ''; } } – mks Aug 23 '20 at 16:38

1 Answers1

0

First you need to find all stages if this is not known up front and can be dynamic. If you have that you can just build the table by iterating your data.

<?php

$data = [
  'person1' => [
    'stage1' => 4,
    'stage2' => 4,
    'stage3' => 4,
    'stage4' => 4,
    'stage5' => 2,
  ],
  'person2' => [
    'stage3' => 1,
  ],
  'person3' => [
    'stage1' => 2,
    'stage2' => 1,
    'stage4' => 1,
    'stage5' => 2,
  ],
];

$stages = [];
foreach ($data as $person) {
  foreach ($person as $stage => $number) {
    if (!in_array($stage, $stages)) {
      $stages[] = $stage;
    }
  }
}

var_dump($stages);

?>
<table>
  <th>person</th>
  <?php foreach ($stages as $stage): ?>
    <th><?php echo $stage; ?></th>
  <?php endforeach ?>
  <?php foreach ($data as $personName => $person): ?>
    <tr>
      <td><?php echo $personName; ?></td>
      <?php foreach ($stages as $stage): ?>
        <td><?php echo $person[$stage] ?? 0; ?></td>
      <?php endforeach; ?>
    </tr>
  <?php endforeach; ?>
</table>

And the result:

array(5) { [0]=> string(6) "stage1" [1]=> string(6) "stage2" [2]=> string(6) "stage3" [3]=> string(6) "stage4" [4]=> string(6) "stage5" }

person  stage1  stage2  stage3  stage4  stage5
person1      4       4       4       4       2
person2      0       0       1       0       0
person3      2       1       0       1       2
blahy
  • 1,294
  • 1
  • 8
  • 9