php - array_uintersect_uassoc

Description

array array_uintersect_uassoc ( array $array1, array $array2 [, array $ ..., callback $data_compare_func, callback $key_compare_func] )
array_uintersect_uassoc() returns an array containing all the values of array1 that are present in all the arguments. Note that the keys are used in the comparison unlike in array_uintersect(). Both the data and the indexes are compared by using separate callback functions.

Example 279. array_uintersect_uassoc() example
<?php

$array1
= array("a" => "green", "b" => "brown", "c" => "blue", "red");

$array2 = array("a" => "GREEN", "B" => "brown", "yellow", "red");
print_r(array_uintersect_uassoc($array1, $array2, "strcasecmp", "strcasecmp"));

?>

The above example will output:
Array
(
   [a] => green
   [b] => brown
)

php - array_uintersect_assoc

Description

array array_uintersect_assoc ( array $array1, array $array2 [, array $ ..., callback $data_compare_func] )
array_uintersect_assoc() returns an array containing all the values of array1 that are present in all the arguments. Note that the keys are used in the comparison unlike in array_uintersect(). The data is compared by using a callback function.

Example 278. array_uintersect_assoc() example
<?php

$array1
= array("a" => "green", "b" => "brown", "c" => "blue", "red");

$array2 = array("a" => "GREEN", "B" => "brown", "yellow", "red");
print_r(array_uintersect_assoc($array1, $array2, "strcasecmp"));

?>

The above example will output:
Array
(
   [a] => green
)

php - array_uintersect

Description

array array_uintersect ( array $array1, array $array2 [, array $ ..., callback $data_compare_func] )
array_uintersect() returns an array containing all the values of array1 that are present in all the arguments. The data is compared by using a callback function.

Example 280. array_uintersect() example
<?php
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");

$array2 = array("a" => "GREEN", "B" => "brown", "yellow", "red");
print_r(array_uintersect($array1, $array2, "strcasecmp"));

?>

The above example will output:
Array
(
   [a] => green
   [b] => brown
   [0] => red
)

php - array_udiff_uassoc

Description

array array_udiff_uassoc ( array $array1, array $array2 [, array $ ..., callback $data_compare_func, callback $key_compare_func] )
array_udiff_uassoc() returns an array containing all the values from array1 that are not present in any of the other arguments. Note that the keys are used in the comparison unlike array_diff() and array_udiff(). The comparison of arrays' data is performed by using an user-supplied callback : data_compare_func. In this aspect the behaviour is opposite to the behaviour of array_diff_assoc() which uses internal function for comparison. The comparison of keys (indices) is done also by the callback function key_compare_func. This behaviour is unlike what array_udiff_assoc() does, since the latter compares the indices by using an internal function.

Example 276. array_udiff_uassoc() example
<?phpclass cr {
 private
$priv_member;
 function
cr($val)
 {
  $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),);
$result = array_udiff_uassoc($a, $b, array("cr", "comp_func_cr"), array("cr", "comp_func_key"));

print_r($result);
?>
The above example will output:
Array
(
   [0.1] => cr Object
       (
           [priv_member:private] => 9
       )

   [0.5] => cr Object
       (
           [priv_member:private] => 12
       )

   [0] => cr Object
       (
           [priv_member:private] => 23
       )
)

php - array_udiff_assoc

Description

array array_udiff_assoc ( array $array1, array $array2 [, array $ ..., callback $data_compare_func] )
array_udiff_assoc() returns an array containing all the values from array1 that are not present in any of the other arguments. Note that the keys are used in the comparison unlike array_diff() and array_udiff(). The comparison of arrays' data is performed by using an user-supplied callback. In this aspect the behaviour is opposite to the behaviour of array_diff_assoc() which uses internal function for comparison.

Example 275. array_udiff_assoc() example
<?phpclass cr {
 private
$priv_member;
 function
cr($val)
 {
  $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;
 }
}
$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),);
$result = array_udiff_assoc($a, $b, array("cr", "comp_func_cr"));

print_r($result);

?>

The above example will output:
Array
(
   [0.1] => cr Object
       (
           [priv_member:private] => 9
       )

   [0.5] => cr Object
       (
           [priv_member:private] => 12
       )

   [0] => cr Object
       (
           [priv_member:private] => 23
       )
)

php - array_udiff

Description

array array_udiff ( array $array1, array $array2 [, array $ ..., callback $data_compare_func] )
array_udiff() returns an array containing all the values of array1 that are not present in any of the other arguments. Note that keys are preserved. For the comparison of the data data_compare_func is used. It 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. This is unlike array_diff() which uses an internal function for comparing the data.

Example 277. array_udiff() example
<?phpclass cr {
 private
$priv_member;
 function
cr($val)
 {
  $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;
 }
}

$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),);
$result = array_udiff($a, $b, array("cr", "comp_func_cr"));

print_r($result);

?>

The above example will output:
Array
(
   [0.5] => cr Object
       (
           [priv_member:private] => 12
       )

   [0] => cr Object
       (
           [priv_member:private] => 23
       )

)

php - array_sum

Description

number array_sum ( array $array )
array_sum() returns the sum of values in an array as an integer or float.

Example 274. array_sum() examples
<?php
$a
= array(2, 4, 6, 8);

echo
"sum(a) = " . array_sum($a) . "\n";

$b = array("a" => 1.2, "b" => 2.3, "c" => 3.4);

echo
"sum(b) = " . array_sum($b) . "\n";

?>

The above example will output:
sum(a) = 20
sum(b) = 6.9

php - array_splice

Description

array array_splice ( array &$input, int $offset [, int $length [, array $replacement]] )
array_splice() removes the elements designated by offset and length from the input array, and replaces them with the elements of the replacement array, if supplied. It returns an array containing the extracted elements. Note that numeric keys in input are not preserved.
If offset is positive then the start of removed portion is at that offset from the beginning of the input array. If offset is negative then it starts that far from the end of the input array.
If length is omitted, removes everything from offset to the end of the array. If length is specified and is positive, then that many elements will be removed. If length is specified and is negative then the end of the removed portion will be that many elements from the end of the array. Tip: to remove everything from offset to the end of the array when replacement is also specified, use count($input) for length.
If replacement array is specified, then the removed elements are replaced with elements from this array. If offset and length are such that nothing is removed, then the elements from the replacement array are inserted in the place specified by the offset. Note that keys in replacement array are not preserved. If replacement is just one element it is not necessary to put array() around it, unless the element is an array itself.
The following statements change the values of $input the same way:

Table 21. array_splice() equivalents
array_push($input, $x, $y) array_splice($input, count($input), 0, array($x, $y))
array_pop($input) array_splice($input, -1)
array_shift($input) array_splice($input, 0, 1)
array_unshift($input, $x, $y) array_splice($input, 0, 0, array($x, $y))
$input[$x] = $y // for arrays where key equals offset array_splice($input, $x, 1, $y)


Returns the array consisting of removed elements.

Example 273. array_splice() examples
<?php
$input
= array("red", "green", "blue", "yellow");

array_splice($input, 2);

// $input is now array("red", "green")
$input = array("red", "green", "blue", "yellow");

array_splice($input, 1, -1);

// $input is now array("red", "yellow")
$input = array("red", "green", "blue", "yellow");

array_splice($input, 1, count($input), "orange");

// $input is now array("red", "orange")
$input = array("red", "green", "blue", "yellow");

array_splice($input, -1, 1, array("black", "maroon"));

// $input is now array("red", "green",

// "blue", "black", "maroon")
$input = array("red", "green", "blue", "yellow");

array_splice($input, 3, 0, "purple");

// $input is now array("red", "green",

// "blue", "purple", "yellow");

?>

php - array_slice

Description

array array_slice ( array $array, int $offset [, int $length [, bool $preserve_keys]] )
array_slice() returns the sequence of elements from the array array as specified by the offset and length parameters.
If offset is non-negative, the sequence will start at that offset in the array. If offset is negative, the sequence will start that far from the end of the array.
If length is given and is positive, then the sequence will have that many elements in it. If length is given and is negative then the sequence will stop that many elements from the end of the array. If it is omitted, then the sequence will have everything from offset up until the end of the array.
Note that array_slice() will reorder and reset the array indices by default. Since PHP 5.0.2, you can change this behaviour by setting preserve_keys to TRUE.

Example 272. array_slice() examples
<?php
$input
= array("a", "b", "c", "d", "e");
$output = array_slice($input, 2); // returns "c", "d", and "e"

$output = array_slice($input, -2, 1); // returns "d"
$output = array_slice($input, 0, 3); // returns "a", "b", and "c"

// note the differences in the array keys

print_r(array_slice($input, 2, -1));
print_r(array_slice($input, 2, -1, true));
?>

The above example will output:

Array
(
    [0] => c
    [1] => d
)
Array
(
    [2] => c
    [3] => d
)

php - array_shift

Description

mixed array_shift ( array &$array )
array_shift() shifts the first value of the array off and returns it, shortening the array by one element and moving everything down. All numerical array keys will be modified to start counting from zero while literal keys won't be touched. If array is empty (or is not an array), NULL will be returned.
Note: This function will reset() the array pointer after use.
Example 271. array_shift() example
<?php
$stack
= array("orange", "banana", "apple", "raspberry");$fruit = array_shift($stack);print_r($stack);

?> 
This would result in $stack having 3 elements left:
Array
(
 [0] => banana
 [1] => apple
 [2] => raspberry
)
 

and orange will be assigned to $fruit