Advertisement
cdsatrian

Associative transpose array 201701210024001

Jan 20th, 2017
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.79 KB | None | 0 0
  1. <?php
  2. $siswa = array
  3. (
  4.     array (
  5.     'nama' => 'joko',
  6.     'kelas' => '3smp',
  7.     'hobi' => 'melukis'
  8.     ),
  9.     array (
  10.     'nama' => 'jokosableng',
  11.     'kelas' => '3smp',
  12.     'hobi' => 'menggambar'
  13.     )
  14. );
  15. function array_trans_assoc($arr) {
  16.     $out = array();
  17.     foreach ($arr as $key => $sub_arr) {
  18.         foreach ($sub_arr as $subkey => $subvalue) {
  19.             $out[$subkey][$key] = $subvalue;
  20.         }
  21.     }
  22.     return $out;
  23. }
  24. echo "<pre>";
  25. print_r(array_trans_assoc($siswa));
  26. /*---- RESULT:
  27. Array
  28. (
  29.     [nama] => Array
  30.         (
  31.             [0] => joko
  32.             [1] => jokosableng
  33.         )
  34.  
  35.     [kelas] => Array
  36.         (
  37.             [0] => 3smp
  38.             [1] => 3smp
  39.         )
  40.  
  41.     [hobi] => Array
  42.         (
  43.             [0] => melukis
  44.             [1] => menggambar
  45.         )
  46. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement