简体   繁体   English

PHP数组值到唯一键

[英]PHP Array values to unique keys

I have an associative array that is built from a MYSQL query result. 我有一个从MYSQL查询结果构建的关联数组。 It has 190 keys which are made up of 10 unique 'name' and 19 unique 'display_name' (2B, 3B, HR, RBI, HR etc), with each 'name' and 'display_name' combination having a 'value'. 它有190个键,由10个唯一的“名称”和19个唯一的“ display_name”(2B,3B,HR,RBI,HR等)组成,每个“名称”和“ display_name”组合都有一个“值”。

    array (size=190)
  0 => 
    array (size=3)
      'name' => string 'Name 1' (length=17)
      'display_name' => string '2B' (length=2)
      'value' => string '10' (length=2)
  1 => 
    array (size=3)
      'name' => string 'Name 2' (length=20)
      'display_name' => string '2B' (length=2)
      'value' => string '7' (length=1)
  2 => 
    array (size=3)
      'name' => string 'Name 3' (length=18)
      'display_name' => string '2B' (length=2)
      'value' => string '5' (length=1)
  3 => 
    array (size=3)
      'name' => string 'Name 4' (length=19)
      'display_name' => string '2B' (length=2)
      'value' => string '3' (length=1)
  4 => 
    array (size=3)
      'name' => string 'Name 5' (length=11)
      'display_name' => string '2B' (length=2)
      'value' => string '4' (length=1)

I would like to build array that has the unique 'display_name' as keys along with the 'name' as a key and the 'value' associated with that. 我想建立一个具有唯一的“ display_name”作为键以及“ name”作为键和与之关联的“ value”的数组。 In other words, if you were visualize this as a table, currently it looks like: 换句话说,如果您将其可视化为表格,则当前看起来像:

name  :   display name :  value
name1     2B              10
name2     2B              7
name3     2B              5
name4     2B              3
name5     2B              4

I would like it to look like: 我希望它看起来像:

name   :  2B   :  3B  :  RBI  :  HR
name1     10      5      7       10
name2     8       6      5       9
name3     9       4      3       5
name4     2       1      1       1
name5     6       2      2       8 

I know I can do this in MYSQL but I would like to know the solution in PHP. 我知道我可以在MYSQL中做到这一点,但我想知道PHP中的解决方案。 I have tried many different things and referenced (among many others): Transposing multidimensional arrays in PHP 我已经尝试了许多不同的方法并进行了引用(以及其他参考): 在PHP中转置多维数组

MYSQL similar solution: Transpose Rows to Headers in MYSQL MYSQL类似的解决方案:将行转置为MYSQL中的标题

The closest that I can get to is this, but it does get me the unique 'display_name' values as keys, rather $fieldHash['display_name'] all 190 values. 我可以找到的最接近的是这个,但是它确实为我获得了唯一的“ display_name”值作为键,而不是$ fieldHash ['display_name']所有190个值。

while ($arr = mysql_fetch_assoc($res)){
$row [] = $arr;
foreach($row as $resultArray){
foreach ( $resultArray as $key=>$val ){
$fieldHash[$key][] = $val; 
}
}
}

In this instance, I know the data model but in the future I would like to be dynamic for any number of 'display_name'. 在这种情况下,我知道数据模型,但将来我希望对任何数量的“ display_name”保持动态。

If it's helpful, here is the MYSQL Query: 如果有帮助,这里是MYSQL查询:

$query = "SELECT teaminfo.name, league_stat_categories.display_name, team_points_season.value
FROM team_points_season, teaminfo, league_stat_categories 
WHERE team_points_season.league_key = '$league_key' AND team_points_season.team_key = teaminfo.team_key AND team_points_season.league_key = league_stat_categories.league_key 
AND team_points_season.stat_id = league_stat_categories.stat_id
GROUP BY league_stat_categories.display_name, team_points_season.team_key";

Thanks for any help! 谢谢你的帮助!

//recieved_arr - your array from example

$new_arr=array();
foreach($recieved_arr as $arr){
   // $arr['name']; $arr['display_name']; $arr['value']; - your values for current element
   $name = $arr['name'];
   $new_arr[$name]['display_name'] = $arr['display_name'];
   $new_arr[$name]['value'] = $arr['value'];

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM