1

如果 k/v 对位于可预测的位置,这可以用 array_slice 解决,不幸的是它们随机分散在整个数组中,我必须创建自己的愚蠢的小方法来重新分配东西。不幸的是,我的 while 循环并没有取消蹲下.... 有(当然有)更好的方法,或者纠正这种行为的方法吗?

用于重新分配和将密钥列入黑名单的循环。

            $reports = array(
                  'inspection_number'   =>"{$array['report_key']}",
                  'customer_number'     =>"{$array['customer_number']}",
                  'customer_division'   =>"{$array['customer_division']}",
                  'report_date'         =>"{$array['report_date']}",
                  'customer'            =>"{$array['customer']}",
                  'location'            =>"{$array['location']}",
                  'region'              =>"{$array['region']}",
            );

            while (list($k, $v) = each($reports)) {
                if($array[$k]) { unset($array[$k], $array[$v]); }
            }

数组,你可以看到 $array 仍然有 $reports 键。

array (
  'shipper' => '',
  'status' => '',
  'po' => '',
  'location' => '',
  'inspector' => '',
  'commodity' => '',
  'count' => '',
  'size' => '',
  'label' => '',
  'variety' => '',
  'pack_date' => '',
  'comments' => '',
  'report_key' => '',
  'region' => '',
  'type' => 'melons',
  'report_date' => '1969-12-31',
  'customer_number' => '',
  'customer' => '',
  'customer_division' => '',
  'staged' => 'true',
)array (
  '`inspection_number`' => '\'\'',
  '`customer_number`' => '\'\'',
  '`customer_division`' => '\'\'',
  '`report_date`' => '\'1969-12-31\'',
  '`customer`' => '\'\'',
  '`location`' => '\'\'',
  '`region`' => '\'\'',
)

编辑

如您所见,第二个数组仍然包含第一个数组中的键,这些键没有被取消设置。即使我像这样进行多重递归循环

foreach($reports as $reportsKey => $reportsValue) {
foreach($array as $arrayKey => &$arrayValue) {
    if($reportsKey == $arrayKey) { unset($arrayKey); }
} }

我正在尝试分配 中的键$reports,从 中分配键/值$array,然后从 中取消设置复制的键$arrayarray_slice() 如果键的位置是可预测的,这可以完成$array,但不幸的是它们不是。

奇怪的是,即使尝试分配不存在的键$reports也不起作用

$tmp = array();
foreach($array as $ak => $av) {
    // if $reports['key_name'] does not exist, assign it to a new array.
    if(!$reports[$ak]) { $tmp[$ak] = $av; }
}
4

2 回答 2

0

可能是因为您对相关数组的读取光标位于数组的末尾,您可以使用:

reset($reports);

在运行你的循环之前,虽然我建议使用:

foreach($reports as $k => $v) {
   ...
}

它更优雅一点。

于 2012-02-16T20:17:01.527 回答
0

array_diff_assoc给了我我需要的结果

$reports = array(
      'inspection_number'   =>"{$array['report_key']}",
      'customer_number'     =>"{$array['customer_number']}",
      'customer_division'   =>"{$array['customer_division']}",
      'report_date'         =>"{$array['report_date']}",
      'customer'            =>"{$array['customer']}",
      'location'            =>"{$array['location']}",
      'region'              =>"{$array['region']}",
);

$array = array_diff_assoc($array, $reports);
echo '<pre>'; var_export($array); var_export($reports); echo '</pre>'; break;

array (
  'shipper' => '',
  'status' => '',
  'po' => '',
  'inspector' => '',
  'commodity' => '',
  'count' => '',
  'size' => '',
  'label' => '',
  'variety' => '',
  'pack_date' => '',
  'comments' => '',
  'report_key' => '',
  'type' => 'melons',
  'staged' => 'true',
)

array (
  'inspection_number' => '',
  'customer_number' => '',
  'customer_division' => '',
  'report_date' => '1969-12-31',
  'customer' => '',
  'location' => '',
  'region' => '',
)
于 2012-02-16T21:56:56.727 回答