doujugu1722 2012-02-16 20:10
浏览 70
已采纳

PHP重新分配并从数组中删除部分

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; }
}
  • 写回答

2条回答 默认 最新

  • dousong5161 2012-02-16 21:56
    关注

    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' => '',
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置