PHP 重新分配和删除数组中的部分

发布于 2025-01-06 13:03:20 字数 2393 浏览 1 评论 0原文

如果 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 的键/值, 然后从 $array 中取消设置复制的键。这可以通过 array_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; }
}

This could be solved with array_slice if the k/v pairs were in a predictable place,
unfortunately they've been scattered at random throughout the array and I have to
create my own silly little method to reassign things. Unfortunately my while loop here doesn't unset squat.... Is there (of course there is) a better method, or a way to correct this behavior?

The loop for reassigning and blacklisting keys.

            $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]); }
            }

The arrays, you can see $array still has the $reports keys in it.

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`' => '\'\'',
)

EDIT

So as you can see, the second array STILL contains keys that are in the first array,
these are not being unset. Even if I do a multiple recursion loop like this

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

I am trying to assign the keys in $reports, the keys/values from $array,
then unset the copied keys from $array. This COULD be done with array_slice()
if the position of the keys in $array were predictable, but unfortunately they
aren't.

Strangely even trying to assign the keys that DON'T exist in $reports doesn't work

$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; }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

堇色安年 2025-01-13 13:03:20

可能是因为您所讨论的数组的读取光标位于数组的末尾,所以您可以

reset($reports);

在运行循环之前使用:,尽管我建议使用:

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

它更优雅一点。

Probably because your read cursor for the array in question is at the end of the array, you can use:

reset($reports);

before running your loop, though I'd advise using:

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

Its a bit more elegant.

沫尐诺 2025-01-13 13:03:20

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' => '',
)

array_diff_assoc gave me the results I needed

$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' => '',
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文