0

我有一个多数组,我需要保留前 3 个索引组并从多数组中删除其余的(在每个组中)。

在此处查看多数组: https ://gist.github.com/no1uknow/6887497

所以:在这个例子中,我需要保留多数组:前 3 个 Heavy、Lite、Intermediate 等(这些由 source_type_cd 标识)

保留前 3 个之后的数组 Lite 部分的示例:

 0 => 
    array (size=9)
      'validated_ata' => string '25' (length=2)
      'source_type_cd' => string 'Lite' (length=4)
      'validated_subata' => string '22' (length=2)
      'action_cd' => string '3' (length=1)
      'object_cd' => string '5' (length=1)
      'malfunction_cd' => string '29' (length=2)
      'corrective_action_txt' => string 'Repair-Passenger Seat-Loose / Displaced' (length=39)
      'rec_count' => string '00050' (length=5)
      'group_id' => int 48
  1 => 
    array (size=9)
      'validated_ata' => string '25' (length=2)
      'source_type_cd' => string 'Lite' (length=4)
      'validated_subata' => string '22' (length=2)
      'action_cd' => string '3' (length=1)
      'object_cd' => string '5' (length=1)
      'malfunction_cd' => string '1' (length=1)
      'corrective_action_txt' => string 'Repair-Passenger Seat-Inoperative' (length=33)
      'rec_count' => string '00047' (length=5)
      'group_id' => int 44
  2 => 
    array (size=9)
      'validated_ata' => string '25' (length=2)
      'source_type_cd' => string 'Lite' (length=4)
      'validated_subata' => string '22' (length=2)
      'action_cd' => string '3' (length=1)
      'object_cd' => string '5' (length=1)
      'malfunction_cd' => string '31' (length=2)
      'corrective_action_txt' => string 'Repair-Passenger Seat-Worn / Chaffed / Frayed' (length=45)
      'rec_count' => string '00042' (length=5)
      'group_id' => int 50
4

2 回答 2

0

谢谢科兰达斯,我实际上是这样想出来的……我试图避免很多循环。

在上面的循环中,我将 source_type_cd 设置在一个数组中:

$groups[]=$value['source_type_cd']

接下来,我循环遍历数组并使用 array_splice 两次获取每组的前 3 个,然后合并回一个新数组。

(另外,我正在使用起点和终点($group_count)。)

$start = 0;
$group_count = $i+1;
$top_count = 3;

foreach($groups as $k => $v) {
    $top_count_array = array_merge((array)$top_count_array, (array)array_slice(array_slice($sorted_array, $start, $group_count, true),0,$top_count,true));
    $start = $start+$group_count;
}

var_dump($top_count_array);

再次感谢输入。试图从这么多循环中缩短此代码。获取 $top_count 和 $group_count 的数量的要求也会发生变化……需要一些更动态的东西。:-)

于 2013-10-08T20:13:58.353 回答
0

您只需遍历数组并查看它是否具有任何这些值并将其放入新数组中。

示例($arr您的多阵列在哪里):

// My silly solution for knowing what to look for
// When one is found, it will be removed from the array.
$find = array('Lite','Lite','Lite','Intermediate','Intermediate','Intermediate','Heavy','Heavy','Heavy');

// New array where your the values you want will be placed in
$new_arr = array();

foreach($arr as $v) {
    // No need to keep looking if there's no more to find.
    if(empty($find))
        break;

    // Look in $find array if current "source_type_cd" is still sought-after
    $key = array_search($v['source_type_cd'], $find);
    if($key !== false) {
        $new_arr[] = $v;  // Add to new array
        unset($find[$key]); // Remove from "find" array
    }
}
于 2013-10-08T16:55:31.530 回答