PHP 重新分配和删除数组中的部分
如果 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可能是因为您所讨论的数组的读取光标位于数组的末尾,所以您可以
在运行循环之前使用:,尽管我建议使用:
它更优雅一点。
Probably because your read cursor for the array in question is at the end of the array, you can use:
before running your loop, though I'd advise using:
Its a bit more elegant.
array_diff_assoc 给了我我需要的结果
array_diff_assoc
gave me the results I needed