Exemplo n.º 1
0
 /**
  * (PHP 5)<br/>
  * Computes the intersection of arrays with additional index check, compares indexes by a callback function
  * @link http://php.net/manual/en/function.array-intersect-uassoc.php
  * @param array $array2 <p>
  * The second array.
  * </p>*
  * @param array $_ [optional] <p>
  * Variable list of array arguments to compare values against.
  * </p>
  * @param callable $key_compare_func <p>
  * The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.
  * </p>
  * int<b>callback</b><b>mixed<i>a</i></b><b>mixed<i>b</i></b>
  * @return array the values of <i>array1</i> whose values exist
  * in all of the arguments.
  */
 public function intersect_uassoc(array $array2, array $_ = null, callable $key_compare_func)
 {
     // TODO: use func_get_args() for multiple arguments, like Arr($array)->function($v1, $v2, $v3)
     return array_intersect_uassoc($this->array, $array2, $key_compare_func);
 }
Exemplo n.º 2
0
$names = file(realpath($argv[1]), FILE_IGNORE_NEW_LINES);
echo "And the WINNER is... " . array_rand(array_flip(array_filter(array_reverse(array_column(array_chunk(array_intersect($names, array_replace($names, array_change_key_case(array_fill_keys($names, 'Rick Astley')))), (int) @array_pop(array_fill(array_reduce(array_keys($names), function ($c, $a) {
    return $a;
}), array_unshift($names, 'Rick Astley'), '1 true love'))), array_sum(array_diff_ukey(array_intersect_key(array_diff_assoc(array_uintersect($names, array_intersect_assoc(array_uintersect_assoc($names, array_values($names), function ($a, $b) {
    return $b > $a;
}), $names), function ($a, $b) use($names) {
    return array_shift($names) > $b;
}), array()), array_uintersect_uassoc($names, array_udiff($names, array_diff($names, array_slice(array_merge(array_diff_uassoc(array_udiff_uassoc($names, $names, function ($a, $b) {
    return $b > $a;
}, function ($a, $b) {
    return $b > $a;
}), $names, function ($a, $b) {
    return $a > $b;
}), array_combine($names, $names)), array_product($names), array_search(array_unique(array_count_values(array_merge_recursive($names, array_pad(array_replace_recursive($names, array_intersect_uassoc($names, $names, function ($a, $b) {
    return $a > $b;
})), array_key_exists((int) array_walk($names, function ($v, $k) {
    return $k;
}), $names), array_walk_recursive($names, function ($v, $k) {
    return $k;
}))))), $names))), function ($a, $b) {
    return $a > $b;
}), function ($a, $b) {
    return $b > $a;
}, function ($a, $b) {
    return $b > $a;
})), array_splice($names, array_multisort(array_map(function ($v) {
    return $v;
}, array_intersect_ukey(array_diff_key($names, array_udiff_assoc($names, $names, function ($a, $b) {
    return $a > $b;
})), $names, function ($a, $b) use($names) {
<?php

/* Prototype  : array array_intersect_uassoc(array arr1, array arr2 [, array ...], callback key_compare_func)
 * Description: Computes the intersection of arrays with additional index check, compares indexes by a callback function
 * Source code: ext/standard/array.c
 */
echo "*** Testing array_intersect_uassoc() : usage variation ***\n";
// Initialise function arguments
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "green", "yellow", "red");
$array3 = array("a" => "green", "brown");
// Define error handler
function test_error_handler($err_no, $err_msg, $filename, $linenum, $vars)
{
    if (error_reporting() != 0) {
        // report non-silenced errors
        echo "Error: {$err_no} - {$err_msg}, {$filename}({$linenum})\n";
    }
}
set_error_handler('test_error_handler');
class classWithoutToString
{
}
$value = new classWithoutToString();
var_dump(array_intersect_uassoc($array1, $array2, $value));
var_dump(array_intersect_uassoc($array1, $array2, $array3, $value));
?>
===DONE===
Exemplo n.º 4
0
  <?php 
function myFunction1($v1, $v2)
{
    if ($v1 === $v2) {
        return 0;
    } else {
        if ($v1 > $v2) {
            return 1;
        } else {
            return -1;
        }
    }
}
$a1 = array(0 => "a", 2 => "b", 3 => "c");
$a2 = array(0 => "a", 2 => "c", 3 => "d");
print_r(array_intersect_uassoc($a1, $a2, "myFunction1"));
echo "<br>\n";
array_multisort($a1, SORT_DESC, $a2);
print_r($a1);
echo "<br>\n";
print_r(array_merge_recursive($a1, $a2));
?>
 
   <br>33--------</br>
<?php 
function myfunction2($vi, $v2)
{
    if ($vi === $v2) {
        return "same";
    } else {
        return "different";
Exemplo n.º 5
0
 /**
  * @param HTArray $array2
  * @param $key_compare_func
  * @return $this
  * @desc 带索引检查计算数组的交集,用回调函数比较索引。
  */
 public function array_intersect_uassoc(HTArray $array2, $key_compare_func)
 {
     $this->current = array_intersect_uassoc($this->current, (array) $array2, $key_compare_func);
     return $this;
 }
<?php

/* Prototype  : array array_intersect_uassoc(array arr1, array arr2 [, array ...], callback key_compare_func)
 * Description: Computes the intersection of arrays with additional index check, compares indexes by a callback function
 * Source code: ext/standard/array.c
 */
echo "*** Testing array_intersect_uassoc() : usage variation ***\n";
//Initialize variables
$array1 = array("a" => "green", "c" => "blue", "red");
$array2 = array("a" => "green", "yellow", "red");
// define some class with method
class MyClass
{
    static function static_compare_func($a, $b)
    {
        return strcasecmp($a, $b);
    }
    public function class_compare_func($a, $b)
    {
        return strcasecmp($a, $b);
    }
}
echo "\n-- Testing array_intersect_uassoc() function using class with static method as callback --\n";
var_dump(array_intersect_uassoc($array1, $array2, array('MyClass', 'static_compare_func')));
var_dump(array_intersect_uassoc($array1, $array2, 'MyClass::static_compare_func'));
echo "\n-- Testing array_intersect_uassoc() function using class with regular method as callback --\n";
$obj = new MyClass();
var_dump(array_intersect_uassoc($array1, $array2, array($obj, 'class_compare_func')));
?>
===DONE===
<?php

// array_intersect_uassoc.php
$week = ["Monday" => "Rāhina", "Tuesday" => "Rātū", "Wednesday" => "Rāapa", "Thursday" => "Rāpare", "Friday" => "Rāmere", "Saturday" => "Rāhoroi", "Sunday" => "Rātapu"];
$weekend = ["Saturday" => "Rāhoroi", "Sunday" => "Rātapu"];
$weekendDays = array_intersect_uassoc($week, $weekend, function ($v1, $v2) {
    echo "{$v1}:{$v2}<br>";
    if ($v1 == $v2) {
        return 0;
    }
    return $v1 > $v2 ? 1 : -1;
});
echo "<pre>";
var_dump($weekendDays);
echo "</pre>";
<?php

/* Prototype  : array array_intersect_uassoc(array arr1, array arr2 [, array ...], callback key_compare_func)
 * Description: Computes the intersection of arrays with additional index check, compares indexes by a callback function
 * Source code: ext/standard/array.c
 */
echo "*** Testing array_intersect_uassoc() : usage variation ***\n";
//Initialize variables
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "green", "yellow", "red");
//function name within double quotes
var_dump(array_intersect_uassoc($array1, $array2, "unknown_function"));
//function name within single quotes
var_dump(array_intersect_uassoc($array1, $array2, 'unknown_function'));
//function name without quotes
var_dump(array_intersect_uassoc($array1, $array2, unknown_function));
?>
===DONE===
Exemplo n.º 9
0
 /**
  * Remove all elements from the current array that are not present in the given $array.
  * This function uses array keys for comparison unlike 'intersect' method.
  *
  * @param array  $array Array for comparison
  * @param string $callable Optional callback function that can be uses for comparison.
  *
  * @return $this
  * @throws ArrayObjectException
  */
 public function intersectAssoc($array, $callable = '')
 {
     if ($callable != '') {
         if (!$this->isCallable($callable)) {
             throw new ArrayObjectException(ArrayObjectException::MSG_INVALID_PARAM, ['$callable', 'a callable function or method']);
         }
         $this->val(array_intersect_uassoc($this->val(), $array, $callable));
     } else {
         $this->val(array_intersect_assoc($this->val(), $array));
     }
     return $this;
 }
<?php

/* Prototype  : array array_intersect_uassoc(array arr1, array arr2 [, array ...], callback key_compare_func)
 * Description: Computes the intersection of arrays with additional index check, compares indexes by a callback function
 * Source code: ext/standard/array.c
 */
echo "*** Testing array_intersect_uassoc() : usage variation ***\n";
//Initialize variables
$ref_var = 'a';
$array1 = array('a', $ref_var);
$array2 = array('a' => 1, &$ref_var);
echo "\n-- Testing array_intersect_uassoc() function with referenced variable \$ref_var has value '{$ref_var}' --\n";
var_dump(array_intersect_uassoc($array1, $array2, "strcasecmp"));
// re-assign reference variable to different value
$ref_var = 10;
echo "\n-- Testing array_intersect_uassoc() function with referenced variable \$ref_var value changed to {$ref_var} --\n";
var_dump(array_intersect_uassoc($array1, $array2, "strcasecmp"));
//When array are referenced
$array2 =& $array1;
echo "\n-- Testing array_intersect_uassoc() function when \$array2 is referencd to \$array1 --\n";
var_dump(array_intersect_uassoc($array1, $array2, "strcasecmp"));
?>
===DONE===
Exemplo n.º 11
0
<?php

/*
* array array_intersect_uassoc ( array $array1, array $array2 [, array $..., callback $key_compare_func] )
* Function is implemented in ext/standard/array.c
*/
function key_compare_func($a, $b)
{
    if ($a === $b) {
        return 0;
    }
    return $a > $b ? 1 : -1;
}
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "green", "yellow", "red");
$result = array_intersect_uassoc($array1, $array2, "key_compare_func");
var_dump($result);
Exemplo n.º 12
0
 public function intersect_uassoc($aArray, $callback)
 {
     if ($aArray instanceof MF_PHP_Array) {
         $aArray =& $aArray->to_array();
     }
     $oReturn = new MF_PHP_Array(array_intersect_uassoc($this->__data, $aArray, $callback));
     return $oReturn;
 }
Exemplo n.º 13
0
 /**
  * Get key-value pairs which exist in both of two arrays.
  *
  * Returns an associative array whose key-value pairs exist in both
  * arrays. Uses string comparison for values by default. Optionally
  * provide comparison functions for keys and values.
  *
  * @param array $array1
  * @param array $array2
  * @param callable|null $key_cmp
  * @param callable|null $value_cmp
  * @return array
  */
 public static function pairIntersection($array1, $array2, $key_cmp = null, $value_cmp = null)
 {
     if ($key_cmp === null) {
         if ($value_cmp === null) {
             return array_intersect_assoc($array1, $array2);
         } else {
             return array_uintersect_assoc($array1, $array2, $value_cmp);
         }
     } else {
         if ($value_cmp === null) {
             return array_intersect_uassoc($array1, $array2, $key_cmp);
         } else {
             return array_uintersect_uassoc($array1, $array2, $value_cmp, $key_cmp);
         }
     }
 }
<?php

/* Prototype  : array array_intersect_uassoc(array arr1, array arr2 [, array ...], callback key_compare_func)
 * Description: Computes the intersection of arrays with additional index check, compares indexes by a callback function
 * Source code: ext/standard/array.c
 */
echo "*** Testing array_intersect_uassoc() : usage variation ***\n";
//Initialize variables
$arr1_string_int = array('1', '2');
$arr2_string_int = array('1', '3');
$arr1_string_float = array('1.00', '2.00');
$arr2_string_float = array('1.00', '3.00');
function key_compare_func($a, $b)
{
    if ($a === $b) {
        return 0;
    }
    return $a > $b ? 1 : -1;
}
echo "\n-- Result of strings containing integers intersection --\n";
var_dump(array_intersect_uassoc($arr1_string_int, $arr2_string_int, "key_compare_func"));
echo "\n-- Result of strings containing floating points intersection --\n";
var_dump(array_intersect_uassoc($arr1_string_float, $arr2_string_float, "key_compare_func"));
echo "\n-- Result of strings containing integers and strings containing floating points intersection --\n";
var_dump(array_intersect_uassoc($arr1_string_int, $arr2_string_float, "key_compare_func"));
?>
===DONE===
Exemplo n.º 15
0
 /**
  * @dataProvider arraySet1
  */
 public function testIntersectAssoc2($array)
 {
     $compare = ['k1' => 'v1'];
     // callback function used for filtering
     $callable = function ($a, $b) {
         if ($a == $b) {
             return 0;
         }
         return 1;
     };
     $a = new ArrayObject($array);
     @$a->intersectAssoc($compare, $callable);
     @($array = array_intersect_uassoc($array, $compare, $callable));
     $this->assertSame($array, $a->val());
 }
Exemplo n.º 16
0
<?php

$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "GREEN", "B" => "brown", "yellow", "red");
var_dump(array_intersect_uassoc($array1, $array2, 'strcasecmp'));
Exemplo n.º 17
0
/* Prototype  : array array_intersect_uassoc(array arr1, array arr2 [, array ...], callback key_compare_func)
 * Description: Computes the intersection of arrays with additional index check, compares indexes by a callback function
 * Source code: ext/standard/array.c
 */
echo "*** Testing array_intersect_uassoc() : error conditions ***\n";
// Initialise function arguments
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "green", "yellow", "red");
$array3 = array("a" => "green", "brown");
$extra_arg = 10;
//Callback function
function key_compare_func($a, $b)
{
    if ($a === $b) {
        return 0;
    }
    return $a > $b ? 1 : -1;
}
//Test array_intersect_uassoc with one more than the expected number of arguments
echo "\n-- Testing array_intersect_uassoc() function with more than expected no. of arguments --\n";
var_dump(array_intersect_uassoc($array1, $array2, 'key_compare_func', $extra_arg));
// Testing array_intersect_uassoc with one less than the expected number of arguments
echo "\n-- Testing array_intersect_uassoc() function with less than expected no. of arguments --\n";
var_dump(array_intersect_uassoc($array1, $array2));
var_dump(array_intersect_uassoc($array1, $array2, $array3));
// Testing array_intersect_uassoc with no arguments
echo "\n-- Testing array_intersect_uassoc() function with no arguments --\n";
var_dump(array_intersect_uassoc());
?>
===DONE===
$fp = fopen(__FILE__, "r");
// define some classes
class classWithToString
{
    public function __toString()
    {
        return "Class A object";
    }
}
class classWithoutToString
{
}
// heredoc string
$heredoc = <<<EOT
hello world
EOT;
// add arrays
$index_array = array(1, 2, 3);
$assoc_array = array('one' => 1, 'two' => 2);
//array of values to iterate over
$inputs = array('int 0' => 0, 'int 1' => 1, 'int 12345' => 12345, 'int -12345' => -12345, 'float 10.5' => 10.5, 'float -10.5' => -10.5, 'float 12.3456789000e10' => 123456789000.0, 'float -12.3456789000e10' => -123456789000.0, 'float .5' => 0.5, 'uppercase NULL' => NULL, 'lowercase null' => null, 'lowercase true' => true, 'lowercase false' => false, 'uppercase TRUE' => TRUE, 'uppercase FALSE' => FALSE, 'empty string DQ' => "", 'empty string SQ' => '', 'string DQ' => "string", 'string SQ' => 'string', 'mixed case string' => "sTrInG", 'heredoc' => $heredoc, 'instance of classWithToString' => new classWithToString(), 'instance of classWithoutToString' => new classWithoutToString(), 'undefined var' => @$undefined_var, 'unset var' => @$unset_var, 'resource' => $fp);
// loop through each element of the array for arr1
foreach ($inputs as $key => $value) {
    echo "\n--{$key}--\n";
    var_dump(array_intersect_uassoc($value, $array2, 'key_compare_func'));
    var_dump(array_intersect_uassoc($value, $array2, $array3, 'key_compare_func'));
}
fclose($fp);
?>
===DONE===
Exemplo n.º 19
0
        $this->priv_member = $val;
    }
    function comp_func_cr($a, $b)
    {
        if ($a->priv_member === $b->priv_member) {
            return 0;
        }
        return $a->priv_member > $b->priv_member ? 1 : -1;
    }
    function comp_func_key($a, $b)
    {
        if ($a === $b) {
            return 0;
        }
        return $a > $b ? 1 : -1;
    }
}
$a = array("0.1" => new cr(9), "0.5" => new cr(12), 0 => new cr(23), 1 => new cr(4), 2 => new cr(-15));
$b = array("0.2" => new cr(9), "0.5" => new cr(22), 0 => new cr(3), 1 => new cr(4), 2 => new cr(-15));
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "green", "yellow", "red");
print_r(array_udiff($a, $b, array("cr", "comp_func_cr")));
print_r(array_udiff_assoc($a, $b, array("cr", "comp_func_cr")));
print_r(array_udiff_uassoc($a, $b, array("cr", "comp_func_cr"), array("cr", "comp_func_key")));
print_r(array_diff_uassoc($array1, $array2, "key_compare_func"));
print "------------------------------------\n";
print_r(array_uintersect($a, $b, array("cr", "comp_func_cr")));
print_r(array_uintersect_assoc($a, $b, array("cr", "comp_func_cr")));
print_r(array_uintersect_uassoc($a, $b, array("cr", "comp_func_cr"), array("cr", "comp_func_key")));
print_r(array_intersect_uassoc($array1, $array2, "key_compare_func"));