2015-05-04 81 views
0

在操纵数据我有一个像下面检索具有相同值,并将它们组中的数组元素的组

$crops = array(
    0 => array(
     'crop_name' => 'Maize', 
     'crop_variety_name' => 'Longe 10H', 
     'weeks' => array(
      9 => 11.200, 
      10 => 14.700, 
      11 => 12.300, 
      12 => 4.300, 
      14 => 8.500, 
      16 => 18.800, 
      17 => 10.600, 
      20 => 10.000, 
      30 => 7.000 
    ) 
), 
    1 => array(
    'crop_name' => 'Maize', 
    'crop_variety_name' => 'Longe 5', 
    'weeks' => array(
      15 => 15.400, 
      16 => 4.700, 
      19 => 11.000, 
      20 => 3.000, 
      21 => 5.000, 
      29 => 2.000 
    ) 
), 
    2 => array(
    'crop_name' => 'Maize', 
    'crop_variety_name' => 'VP Max', 
    'weeks' => array(
      9 => 6.800, 
      10 => 8.000, 
      14 => 3.000, 
      15 => 6.800, 
      17 => 4.300, 
      18 => 7.400, 
      20 => 5.900, 
      21 => 2.400, 
      22 => 2.800, 
      23 => 5.400, 
      24 => 3.900 
    ) 
), 
    3 => array(
    'crop_name' => 'Rice', 
    'crop_variety_name' => 'Superica 2', 
    'weeks' => array(
     18 => 6.600, 
     19 => 11.500, 
     20 => 8.300, 
     21 => 10.100, 
     24 => 2.800 
    ) 
), 
4 => array(
    'crop_name' => 'Soya', 
    'crop_variety_name' => 'Soya N1', 
    'weeks' => array(
     20 => 3.000 
    ) 
), 
5 => array(
    'crop_name' => 'Soya', 
    'crop_variety_name' => 'Soya N3', 
    'weeks' => array(
     10 => 5.9, 
     11 => 12.800, 
     12 => 5.100, 
     15 => 4.000, 
     19 => 4.000, 
     31 => 3.100 
    ) 
    ) 
); 

不同作物crop_name具有一个或多个品种crop_variety_name的阵列。我想检索庄稼,例如`'crop_name'=>'玉米',无论他们的作物品种如何,然后检索星期数组,并为每个作物品种添加周数组中的所有值,以便我有一个类似数组此

array(
    'Maize' => 195.2, 
    'Rice' => 39.3 
    'Soya' => 37.9) 

当关键是crop_name和的值是总的所述周阵列每种作物的crop_varieties中的值的。第一个数组可以包含任意数量的作物,crop_varieties,星期数组可以包含任意数量的值。我该如何解决这个问题。对于作物的名字我想这

$crop_names = array(); 
      for($i = 0; $i < count($crops); $i++){ 
      array_push($crop_names, $crops[$i]['crop_name']); 
       } 
       $crop_name = array_values(array_unique($crop_names)); 

此作品为crop_names但array_unique导致数据丢失。

+0

什么是数据丢失?它删除重复项,但仍保留每种类型的一个项目。 – axiac

+0

@axiac它将保持每个值遇到的第一个键,并忽略所有后续键。 –

+0

你的代码中没有地方使用'$ crop'的键。偶然的情况是,你在'$ crop_names'中输入的值使用'$ crop'中的相同键。如果你这样做,就不会再发生这种情况了,比如'$ crop_names = array();'之前的'unset($ crop [2])''。 – axiac

回答

1

您的代码与您的要求不符。

我要检索的作物例如`“crop_name” =>“玉米”不论其作物品种,然后检索周阵列和添加的所有值的一周阵列中的每个的作物品种,例如我有这样的

array(
    'Maize' => 195.2, 
    'Rice' => 39.3 
    'Soya' => 37.9) 

这是上面的要求是如何转化为代码的数组:

$amounts = array(); 
foreach ($crops as $crop) { 
    $name = $crop['crop_name']; 
    if (! isset($amounts[$name])) { 
     // This is the first time when this crop type is processed 
     $amounts[$name] = 0; 
    } 
    // Add all the values in the week array regardless of varieties 
    $amounts[$name] += array_sum($crop['weeks']); 
} 

// If you need the names in a separate list you can get it with array_keys() 
$crop_names = array_keys($amounts); 

看看该array functions有关更多创意的PHP手册部分。

+0

是的,它完美的作品。这正是我想要实现的。 –

相关问题