Advertisement
cecepsuwanda

pivot

Jan 23rd, 2022
1,064
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.         //Enter your code here, enjoy!
  3.  
  4. $data = array("x"=>["a"=>1],"y"=>["a"=>2,"b"=>3],"z"=>["a"=>5,"c"=>8]);
  5.  
  6. $pivot = array();
  7. $jdl_kolom = array();
  8. $jdl_baris = array();
  9. foreach($data as $key => $value)
  10. {
  11.   if (!in_array($key,$jdl_kolom))
  12.   {
  13.     $jdl_kolom[]=$key;  
  14.   }
  15.   foreach($value as $key1 => $value1)
  16.   {
  17.     if (!in_array($key1,$jdl_baris))
  18.     {
  19.       $jdl_baris[]=$key1;  
  20.     }      
  21.     $pivot[$key1][$key]=$value1;
  22.   }
  23. }
  24.  
  25.  
  26. echo "<table>";
  27. echo "<thead>";
  28. echo "<th>";
  29. echo "<td>name</td>";
  30. foreach($jdl_kolom as $jdl)
  31. {
  32.    echo "<td>$jdl</td>";
  33. }
  34. echo "</th>";
  35. echo "</thead>";
  36. echo "<tbody>";
  37. foreach($jdl_baris as $baris)
  38. {
  39.   echo "<tr>";  
  40.   echo "<td>$baris</td>";
  41.   foreach($jdl_kolom as $jdl)
  42.   {
  43.      if(!in_array($jdl,array_keys($pivot[$baris]))){
  44.         echo "<td>0</td>";
  45.      }else{
  46.         echo "<td>".$pivot[$baris][$jdl]."</td>";
  47.      }
  48.   }
  49.   echo "</tr>";  
  50. }
  51. echo "</tbody>";
  52.  
  53. echo "</table>";
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement