0

I want to make a table like the attachment, can someone help me? I'm confused on how to make the table look like the image, can I remove the second data if inside the array there are some duplicate values?

Before:

Before

After:

Final Result

This is how my code looks like:

<?php

$init_array = array(
    array('kode_pemesanan'=>'FPS26122018001', 
        'nama'=>'bintang', 
        'value1'=>'A Pieu pastel blusher (Shade 1)', 
        'value2'=>'1', 
        'value3'=>'50000', 
        'value4'=> '9000',
        'value5'=> '59000',
    ),
    array('kode_pemesanan'=>'FPS26122018001', 
        'nama'=>'bintang', 
        'value1'=>'A Pieu pastel blusher (Shade 3)', 
        'value2'=>'1', 
        'value3'=>'10000', 
        'value4'=> '9000',
        'value5'=> '59000',
    ),
    array('kode_pemesanan'=>'FPS26122018002', 
        'nama'=>'bintang', 
        'value1'=>'A Pieu pastel blusher (Shade 3)', 
        'value2'=>'1', 
        'value3'=>'10000', 
        'value4'=> '9000',
        'value5'=> '59000',
    ),
);

$formatted_array = array();

foreach($init_array as $element){
    $formatted_array[$element['kode_pemesanan']][] = $element;
}
?>

<table border='2' width="70%" align="center">
 <? foreach($formatted_array as $row ): ?>
   <tr>
     <td rowspan="<?=count($row)?>"><?=$row[0]['kode_pemesanan']?></td>
     <td rowspan="<?=count($row)?>"><?=$row[0]['nama']?></td>
     <? foreach( $row as $value ): ?>
      <td><?=$value['value1']?></td>
      <td><?=$value['value2']?></td>
      <td><?=$value['value3']?></td>
      <td><?=$value['value4']?></td>
      <td><?=$value['value5']?></td>
      </tr><tr>
     <? endforeach; ?>
   </tr>
 <? endforeach; ?>
</table>

1 Answer 1

1

Just try array_unique()

print_r(array_unique($init_array));

So you can try like this. Put this line after your array $init_array it will remove all duplicate records.

$init_array = array_unique($init_array);

Let me know if this is helping you.

Try This Code then

<?php
$init_array = array(
array('kode_pemesanan'=>'FPS26122018001', 
    'nama'=>'bintang', 
    'value1'=>'A Pieu pastel blusher (Shade 1)', 
    'value2'=>'1', 
    'value3'=>'50000', 
    'value4'=> '9000',
    'value5'=> '59000',
),
array('kode_pemesanan'=>'FPS26122018001', 
    'nama'=>'bintang', 
    'value1'=>'A Pieu pastel blusher (Shade 3)', 
    'value2'=>'1', 
    'value3'=>'10000', 
    'value4'=> '9000',
    'value5'=> '59000',
),
array('kode_pemesanan'=>'FPS26122018002', 
    'nama'=>'bintang', 
    'value1'=>'A Pieu pastel blusher (Shade 3)', 
    'value2'=>'1', 
    'value3'=>'10000', 
    'value4'=> '9000',
    'value5'=> '59000',
),
);

$formatted_array = array();

foreach($init_array as $element){
$formatted_array[$element['kode_pemesanan']][] = $element;
}
?>

<table border='2' width="70%" align="center">
<?php foreach($formatted_array as $row ): ?>
<tr>
 <td rowspan="<?=count($row)?>"><?=$row[0]['kode_pemesanan']?></td>
 <td rowspan="<?=count($row)?>"><?=$row[0]['nama']?></td>
 <?php foreach( $row as $key => $value ): ?>
  <td><?=$value['value1']?></td>
  <td><?=$value['value2']?></td>
  <td><?=$value['value3']?></td>
  <?php if($key == 0): ?>
  <td rowspan="<?=count($row)?>"><?=$value['value4']?></td>
  <td rowspan="<?=count($row)?>"><?=$value['value5']?></td>
  <?php endif; ?>
  </tr><tr>
 <?php endforeach; ?>
 </tr>
 <?php endforeach; ?>
 </table>
Sign up to request clarification or add additional context in comments.

1 Comment

thank for your help but the result not like the final image output >

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.