如果在多维数组中具有相同id,则将某些数据合并到一个数组中



我有一个多维数组$voucher_data与以下数组:

array(6) {
  [0]=>
  array(5) {
    ["voucher_id"]=>
    string(1) "1"
    ["menu_id"]=>
    string(3) "521"
    ["qty"]=>
    string(1) "2"
    ["variant_menu_id"]=>
    string(1) "2"
    ["voucher_menu_id"]=>
    string(1) "3"
  }
  [1]=>
  array(5) {
    ["voucher_id"]=>
    string(1) "1"
    ["menu_id"]=>
    string(3) "521"
    ["qty"]=>
    string(1) "2"
    ["variant_menu_id"]=>
    string(1) "6"
    ["voucher_menu_id"]=>
    string(1) "3"
  }
  [2]=>
  array(5) {
    ["voucher_id"]=>
    string(1) "1"
    ["menu_id"]=>
    string(3) "521"
    ["qty"]=>
    string(1) "1"
    ["variant_menu_id"]=>
    string(1) "1"
    ["voucher_menu_id"]=>
    string(1) "4"
  }
  [3]=>
  array(5) {
    ["voucher_id"]=>
    string(1) "1"
    ["menu_id"]=>
    string(3) "521"
    ["qty"]=>
    string(1) "1"
    ["variant_menu_id"]=>
    string(1) "6"
    ["voucher_menu_id"]=>
    string(1) "4"
  }
  [4]=>
  array(5) {
    ["voucher_id"]=>
    string(1) "1"
    ["menu_id"]=>
    string(3) "519"
    ["qty"]=>
    string(1) "2"
    ["variant_menu_id"]=>
    NULL
    ["voucher_menu_id"]=>
    string(1) "1"
  }
  [5]=>
  array(5) {
    ["voucher_id"]=>
    string(1) "1"
    ["menu_id"]=>
    string(3) "525"
    ["qty"]=>
    string(1) "3"
    ["variant_menu_id"]=>
    NULL
    ["voucher_menu_id"]=>
    string(1) "2"
  }
}

在我的数组[0][1]中,voucher_menu_id3。它们有相同的voucher_menu_id[2] [3]4

所以我想把variant_menu_id变成1数组变成这样:

array(4) {
  [0]=>
  array(5) {
    ["voucher_id"]=>
    string(1) "1"
    ["menu_id"]=>
    string(3) "521"
    ["qty"]=>
    string(1) "2"
    ["variant_menu_id"]=>
    array(2){
      string(1) "2"
      string(1) "6"
    }
    ["voucher_menu_id"]=>
    string(1) "3"
  }
  [1]=>
  array(5) {
    ["voucher_id"]=>
    string(1) "1"
    ["menu_id"]=>
    string(3) "521"
    ["qty"]=>
    string(1) "1"
    ["variant_menu_id"]=>
    array(2){
      string(1) "1"
      string(1) "4"
    }
    ["voucher_menu_id"]=>
    string(1) "4"
  }
  }
  [2]=>
  array(5) {
    ["voucher_id"]=>
    string(1) "1"
    ["menu_id"]=>
    string(3) "519"
    ["qty"]=>
    string(1) "2"
    ["variant_menu_id"]=>
    NULL
    ["voucher_menu_id"]=>
    string(1) "1"
  }
  [3]=>
  array(5) {
    ["voucher_id"]=>
    string(1) "1"
    ["menu_id"]=>
    string(3) "525"
    ["qty"]=>
    string(1) "3"
    ["variant_menu_id"]=>
    NULL
    ["voucher_menu_id"]=>
    string(1) "2"
  }
}

伙计们,你们能帮我怎么把它组合起来吗?

谢谢(:

就像上面的注释一样,只需使用voucher_menu_id作为分配新容器的键。使用它,当你loop:

时,它将是group
$new_array = array();
foreach ($voucher_data as $v) {
    if(empty($new_array[$v['voucher_menu_id']])) { // if empty initialize
        $new_array[$v['voucher_menu_id']] = $v; // base array group
        $new_array[$v['voucher_menu_id']]['variant_menu_id'] = array(); // intialize container
    }
    // push id
    $new_array[$v['voucher_menu_id']]['variant_menu_id'][] = $v['variant_menu_id'];
}
$new_array = array_values($new_array); // array reindex

未经测试,但这样的东西可能会起作用:

$output = array();
foreach ($array as $entry) {
    // make the variant_menu_id an array
    $entry['variant_menu_id'] = (array) $entry['variant_menu_id'];
    $key = $entry['voucher_menu_id'];
    if (!array_key_exists($key, $output)) {
        // create a new entry in the output array using the entire $entry
        $output[$key] = $entry;
        continue;
    }
    // otherwise, add your variant menu ID to the existing values
    $output[$key]['variant_menu_id'] = array_merge($output[$key]['variant_menu_id'], $entry['variant_menu_id']);
}

这将通过按voucher_menu_id对条目进行分组,并将variant_menu_id的值添加到$output中的数组中来创建一个新的数组。

相关内容

  • 没有找到相关文章