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条)

报告相同问题?

悬赏问题

  • ¥20 Java-Oj-桌布的计算
  • ¥15 请问如何在openpcdet上对KITTI数据集的测试集进行结果评估?
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路