note 119307 added to function.array-diff

From: Date: Tue, 10 May 2016 15:49:51 +0000
Subject: note 119307 added to function.array-diff
Groups: php.notes 
Request: Send a blank email to php-notes+get-205451@lists.php.net to get a copy of this message
Resubmitting... the update for takes into account comparison issues Computes the difference of all the arrays <?php /** * array_diffs — Computes the difference of all the arrays * * @param array * * array1 - The array to compare from and against * array2 - The array to compare from and against * array(n) - More arrays to compare from and against * * @return array Returns all the arrays that do contains entries that cannot be matched in any of the arrays. */ function array_diffs() { $count = func_num_args(); if($count < 2) { trigger_error('Must provide at least 2 arrays for comparison.'); } $check = array(); $out = array(); //resolve comparison issue $func = function($a, $b) { $dbl = function($i, $d) { $e = 0.00001; if(abs($i-$d) < $e) { return true; } return false; }; if((gettype($a) == 'integer' && gettype($b['value']) == 'double') || (gettype($a) == 'double' && gettype($b['value']) == 'integer')) { if((gettype($a) == 'integer' && $dbl((double) $a, $b['value'])) || (gettype($b['value']) == 'integer' && $dbl((double) $b['value'], $a))) { return true; } } elseif((gettype($a) == 'double') && (gettype($b['value']) == 'double')) { return $dbl($a,$b['value']); } elseif($a == $b['value']) { return true; } return false; }; for($i = 0; $i < $count; $i++) { if(!is_array(func_get_arg($i))) { trigger_error('Parameters must be passed as arrays.'); } foreach(func_get_arg($i) as $key => $value) { if(is_numeric($key) && is_string($value)) { if(array_key_exists($value, $check) && $func($value, $check[$value])) { $check[$value]['count'] = $check[$value]['count'] + 1; } else { $check[$value]['value'] = $value; $check[$value]['count'] = 1; } } elseif(is_numeric($key) && (is_bool($value) || is_null($value) || is_numeric($value) || is_object($value) || is_resource($value))) { $update = false; foreach($check as $check_key => $check_value) { if(is_numeric($key) && (is_bool($check_value['value']) || is_null($check_value['value']) || is_numeric($check_value['value']) || is_object($check_value['value']) || is_resource($check_value['value'])) && $func($value, $check_value)) { $update = true; $check[$check_key]['count'] = $check[$check_key]['count'] + 1; } } if(!$update) { $check[] = array('value' => $value, 'count' => 1); } } else { if(array_key_exists($key, $check) && $func($value, $check[$key])) { $check[$key]['count'] = $check[$key]['count'] + 1; } else { $check[$key]['value'] = $value; $check[$key]['count'] = 1; } } } } foreach($check as $check_key => $check_value) { if($check_value['count'] == 1) { for ($i = 0; $i < $count; $i++) { foreach(func_get_arg($i) as $key => $value) { if(is_numeric($key) && is_string($value) && ($value == (string) $check_key)) { $out[$i][$key] = $value; } elseif(is_numeric($key) && ($check_value['value'] == $value)) { $out[$i][$key] = $value; } elseif(is_string($key) && ($check_value['value'] == $value)) { $out[$i][$key] = $value; } } } } } return $out; } ?> ---- Server IP: 72.52.91.14 Probable Submitter: 41.190.12.169 ---- Manual Page -- http://php.net/manual/en/function.array-diff.php Edit -- https://master.php.net/note/edit/119307 Del: integrated -- https://master.php.net/note/delete/119307/integrated Del: useless -- https://master.php.net/note/delete/119307/useless Del: bad code -- https://master.php.net/note/delete/119307/bad+code Del: spam -- https://master.php.net/note/delete/119307/spam Del: non-english -- https://master.php.net/note/delete/119307/non-english Del: in docs -- https://master.php.net/note/delete/119307/in+docs Del: other reasons-- https://master.php.net/note/delete/119307 Reject -- https://master.php.net/note/reject/119307 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#205451) next »