Guest User

Untitled

a guest
Dec 15th, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.73 KB | None | 0 0
  1. $results = array(
  2.     array(
  3.         'id' => 0,
  4.         'key' => 'somekey',
  5.         'value' => 'This is a string.'
  6.     ),
  7.     array(
  8.         'id' => 0,
  9.         'key' => 'someotherkey',
  10.         'value' => 100002
  11.     ),
  12.     array(
  13.         'id' => 1,
  14.         'key' => 'someotherkey',
  15.         'value' => 100002
  16.     ),
  17.     array(
  18.         'id' => 2,
  19.         'key' => 'somekey',
  20.         'value' => 'This is another string'
  21.     ),
  22.     array(
  23.         'id' => 2,
  24.         'key' => 'someotherkey',
  25.         'value' => 100002
  26.     ),
  27. ); //This is like what you get from MySQL.  Each array is a result from the database.
  28.  
  29. $crosstab = array();
  30.  
  31. foreach($results as $result) {
  32.     if(!is_array($crosstab[$result['key']]) {
  33.         $crosstab[$result['key']] = array();
  34.     }
  35.     $crosstab[$result['key']][$result[id]] = $result['value'];
  36. }
  37.  
  38. var_dump($crosstab);
Add Comment
Please, Sign In to add comment