1

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; }
}
3
  • don't get what you need and what's your problem
    – dynamic
    Feb 16, 2012 at 20:12
  • Does the reports array key really contain backticks? Ugh!
    – Mark Baker
    Feb 16, 2012 at 20:19
  • The backticks are temporary to prep for DB insertion
    – ehime
    Feb 16, 2012 at 20:33

2 Answers 2

0

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.

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.