Longhorn PHP 2025 - Call For Papers

Voting

: min(two, two)?
(Example: nine)

The Note You're Voting On

emeka at echeruo at gmail dot com
9 years ago
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 issues in PHP
$func = function($a, $b) {
if(
gettype($a) == 'integer' && gettype($b['value']) == 'double' || gettype($a) == 'double' && gettype($b['value']) == 'integer') {
if(
gettype($a) == 'integer') {
if((double)
$a == $b['value']) {
return
true;
}
} else {
if((double)
$b['value'] == $a) {
return
true;
}
}
} elseif(
gettype($a) == 'double' && gettype($b['value']) == 'double') {
$epsilon = 0.00001;
if(
abs($a - $b['value']) < $epsilon) {
return
true;
}
} else {
if(
$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']))) {
if(
$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)) {
if(
$value == (string) $check_key) {
$out[$i][$key] = $value;
}
} elseif(
is_numeric($key) && (is_bool($value) || is_null($value) || is_numeric($value) || is_object($value) || is_resource($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']))) {
if(
$check_value['value'] == $value) {
$out[$i][$key] = $value;
}
}
} else {
if(
$key == $check_key) {
$out[$i][$key] = $value;
}
}
}
}
}
}
return
$out;
}

?>

<< Back to user notes page

To Top