Spl/misc-Phpdoc专题
各种类及接口
目录
- ArrayObject — The ArrayObject class
- ArrayObject::append — 追加新的值作为最后一个元素。
- ArrayObject::asort — Sort the entries by value
- ArrayObject::__construct — Construct a new array object
- ArrayObject::count — 统计 ArrayObject 内 public 属性的数量
- ArrayObject::exchangeArray — Exchange the array for another one
- ArrayObject::getArrayCopy — Creates a copy of the ArrayObject
- ArrayObject::getFlags — Gets the behavior flags
- ArrayObject::getIterator — Create a new iterator from an ArrayObject instance
- ArrayObject::getIteratorClass — Gets the iterator classname for the ArrayObject
- ArrayObject::ksort — Sort the entries by key
- ArrayObject::natcasesort — Sort an array using a case insensitive "natural order" algorithm
- ArrayObject::natsort — Sort entries using a "natural order" algorithm
- ArrayObject::offsetExists — Returns whether the requested index exists
- ArrayObject::offsetGet — Returns the value at the specified index
- ArrayObject::offsetSet — 为指定索引设定新的值
- ArrayObject::offsetUnset — Unsets the value at the specified index
- ArrayObject::serialize — Serialize an ArrayObject
- ArrayObject::setFlags — Sets the behavior flags
- ArrayObject::setIteratorClass — Sets the iterator classname for the ArrayObject
- ArrayObject::uasort — Sort the entries with a user-defined comparison function and maintain key association
- ArrayObject::uksort — Sort the entries by keys using a user-defined comparison function
- ArrayObject::unserialize — Unserialize an ArrayObject
- SplObserver — The SplObserver
interface
- SplObserver::update — Receive update from subject
- SplSubject — The SplSubject interface
- SplSubject::attach — Attach an SplObserver
- SplSubject::detach — Detach an observer
- SplSubject::notify — Notify an observer
无法归入其它 SPL 分类的类和接口。
简介
This class allows objects to work as arrays.
类摘要
ArrayObject
class ArrayObject <span class="oointerface">implements <span class="interfacename">IteratorAggregate <span class="oointerface">, ArrayAccess , <span class="interfacename">Serializable <span class="oointerface">, Countable {
/* 常量 */
const int
STD_PROP_LIST = 1 ;
const int
ARRAY_AS_PROPS = 2 ;
/* 方法 */
public <span
class="methodname">__construct ([ <span
class="methodparam">mixed $input<span
class="initializer"> = array() [, <span
class="methodparam">int $flags<span
class="initializer"> = 0 [, <span
class="methodparam">string
$iterator_class =
"ArrayIterator" ]]] )
public void
append ( <span
class="type">mixed $value )
public void
asort ([ <span
class="methodparam">int $flags<span
class="initializer"> = SORT_REGULAR ] )
public int <span class="methodname">count ( void )
public array
exchangeArray ( <span
class="methodparam">mixed $input )
public array getArrayCopy ( <span class="methodparam">void )
public int <span class="methodname">getFlags ( <span class="methodparam">void )
public <span class="type">ArrayIterator <span class="methodname">getIterator ( <span class="methodparam">void )
public string getIteratorClass ( <span class="methodparam">void )
public void
ksort ([ <span
class="methodparam">int $flags<span
class="initializer"> = SORT_REGULAR ] )
public void natcasesort ( <span class="methodparam">void )
public void natsort ( <span class="methodparam">void )
public bool
offsetExists ( <span
class="methodparam">mixed $index )
public mixed
offsetGet ( <span
class="methodparam">mixed $index )
public void
offsetSet ( <span
class="methodparam">mixed $index ,
mixed
$newval )
public void
offsetUnset ( <span
class="methodparam">mixed $index )
public string serialize ( <span class="methodparam">void )
public void
setFlags ( <span
class="methodparam">int $flags )
public void
setIteratorClass ( <span
class="methodparam">string
$iterator_class )
public void
uasort ( <span
class="type">callable $cmp_function )
public void
uksort ( <span
class="type">callable $cmp_function )
public void
unserialize ( <span
class="methodparam">string
$serialized )
}
预定义常量
ArrayObject Flags
ArrayObject::STD_PROP_LIST
Properties of the object have their normal functionality when accessed
as list (var_dump, foreach, etc.).
ArrayObject::ARRAY_AS_PROPS
Entries can be accessed as properties (read and write).
更新日志
| 版本 | 说明 |
|---|---|
| 5.3.0 | Implements Serializable. |
ArrayObject::append
追加新的值作为最后一个元素。
说明
public void
ArrayObject::append ( <span
class="methodparam">mixed $value )
追加新的值作为最后一个元素。
Note:
当 ArrayObject 从 object 初始化时不能调用此方法。使用 <span class="methodname">ArrayObject::offsetSet 代替。
参数
value
将要被追加的值
返回值
没有返回值。
范例
示例 #1 ArrayObject::append 例子
<?php
$arrayobj = new ArrayObject(array('first','second','third'));
$arrayobj->append('fourth');
$arrayobj->append(array('five', 'six'));
var_dump($arrayobj);
?>
以上例程会输出:
object(ArrayObject)#1 (5) {
[0]=>
string(5) "first"
[1]=>
string(6) "second"
[2]=>
string(5) "third"
[3]=>
string(6) "fourth"
[4]=>
array(2) {
[0]=>
string(4) "five"
[1]=>
string(3) "six"
}
}
参见
- ArrayObject::offsetSet
ArrayObject::asort
Sort the entries by value
说明
public void
ArrayObject::asort ([ <span
class="methodparam">int $flags<span
class="initializer"> = SORT_REGULAR ] )
Sorts the entries such that the keys maintain their correlation with the entries they are associated with. This is used mainly when sorting associative arrays where the actual element order is significant.
参数
flags
The optional second parameter flags may be used to modify the sorting
behavior using these values:
Sorting type flags:
SORT_REGULAR- compare items normally; the details are described in the comparison operators sectionSORT_NUMERIC- compare items numericallySORT_STRING- compare items as strings-
SORT_LOCALE_STRING- compare items as strings, based on the current locale. It uses the locale, which can be changed using setlocale -
SORT_NATURAL- compare items as strings using "natural ordering" like natsort -
SORT_FLAG_CASE- can be combined (bitwise OR) withSORT_STRINGorSORT_NATURALto sort strings case-insensitively
返回值
没有返回值。
范例
示例 #1 ArrayObject::asort example
<?php
$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
$fruitArrayObject = new ArrayObject($fruits);
$fruitArrayObject->asort();
foreach ($fruitArrayObject as $key => $val) {
echo "$key = $val\n";
}
?>
以上例程会输出:
c = apple
b = banana
d = lemon
a = orange
The fruits have been sorted in alphabetical order, and the key associated with each entry has been maintained.
参见
- ArrayObject::ksort
- ArrayObject::natsort
- ArrayObject::natcasesort
- ArrayObject::uasort
- ArrayObject::uksort
ArrayObject::__construct
Construct a new array object
说明
public <span
class="methodname">ArrayObject::__construct ([ <span
class="methodparam">mixed $input<span
class="initializer"> = array() [, <span
class="methodparam">int $flags<span
class="initializer"> = 0 [, <span
class="methodparam">string
$iterator_class =
"ArrayIterator" ]]] )
This constructs a new array object.
参数
input
The input parameter accepts an array or an
Object.
flags
Flags to control the behaviour of the <span
class="classname">ArrayObject object. See <span
class="methodname">ArrayObject::setFlags.
iterator_class
Specify the class that will be used for iteration of the <span
class="classname">ArrayObject object.
返回值
Returns an ArrayObject object on success.
错误/异常
Throws InvalidArgumentException when:
-
inputis not an array or object -
flagsis not an integer -
iterator_classis not an object that implements Iterator
范例
示例 #1 ArrayObject::__construct example
<?php
$array = array('1' => 'one',
'2' => 'two',
'3' => 'three');
$arrayobject = new ArrayObject($array);
var_dump($arrayobject);
?>
以上例程会输出:
object(ArrayObject)#1 (3) {
[1]=>
string(3) "one"
[2]=>
string(3) "two"
[3]=>
string(5) "three"
}
参见
- ArrayObject::setflags
ArrayObject::count
统计 ArrayObject 内 public 属性的数量
说明
public int <span class="methodname">ArrayObject::count ( <span class="methodparam">void )
获取 ArrayObject 的 public 属性数量
参数
此函数没有参数。
返回值
对象 ArrayObject 的 public 属性数量
Note:
当对象 ArrayObject 是从数组构造而来时,所有属性都是 public 的。
范例
示例 #1 ArrayObject::count 例子
<?php
class Example {
public $public = 'prop:public';
private $prv = 'prop:private';
protected $prt = 'prop:protected';
}
$arrayobj = new ArrayObject(new Example());
var_dump($arrayobj->count());
$arrayobj = new ArrayObject(array('first','second','third'));
var_dump($arrayobj->count());
?>
以上例程会输出:
int(1)
int(3)
ArrayObject::exchangeArray
Exchange the array for another one
说明
public array
ArrayObject::exchangeArray ( <span
class="methodparam">mixed $input )
Exchange the current array with another <span class="type">array or object.
参数
input
The new array or <span
class="type">object to exchange with the current array.
返回值
Returns the old array.
范例
示例 #1 ArrayObject::exchangeArray example
<?php
// Array of available fruits
$fruits = array("lemons" => 1, "oranges" => 4, "bananas" => 5, "apples" => 10);
// Array of locations in Europe
$locations = array('Amsterdam', 'Paris', 'London');
$fruitsArrayObject = new ArrayObject($fruits);
// Now exchange fruits for locations
$old = $fruitsArrayObject->exchangeArray($locations);
print_r($old);
print_r($fruitsArrayObject);
?>
以上例程会输出:
Array
(
[lemons] => 1
[oranges] => 4
[bananas] => 5
[apples] => 10
)
ArrayObject Object
(
[0] => Amsterdam
[1] => Paris
[2] => London
)
ArrayObject::getArrayCopy
Creates a copy of the ArrayObject
说明
public array ArrayObject::getArrayCopy ( <span class="methodparam">void )
Exports the ArrayObject to an array.
参数
此函数没有参数。
返回值
Returns a copy of the array. When the <span class="classname">ArrayObject refers to an object, an array of the properties of that object will be returned.
范例
示例 #1 ArrayObject::getArrayCopy example
<?php
// Array of available fruits
$fruits = array("lemons" => 1, "oranges" => 4, "bananas" => 5, "apples" => 10);
$fruitsArrayObject = new ArrayObject($fruits);
$fruitsArrayObject['pears'] = 4;
// create a copy of the array
$copy = $fruitsArrayObject->getArrayCopy();
print_r($copy);
?>
以上例程会输出:
Array
(
[lemons] => 1
[oranges] => 4
[bananas] => 5
[apples] => 10
[pears] => 4
)
ArrayObject::getFlags
Gets the behavior flags
说明
public int <span class="methodname">ArrayObject::getFlags ( <span class="methodparam">void )
Gets the behavior flags of the <span class="classname">ArrayObject. See the ArrayObject::setFlags method for a list of the available flags.
参数
此函数没有参数。
返回值
Returns the behavior flags of the ArrayObject.
范例
示例 #1 ArrayObject::getFlags example
<?php
// Array of available fruits
$fruits = array("lemons" => 1, "oranges" => 4, "bananas" => 5, "apples" => 10);
$fruitsArrayObject = new ArrayObject($fruits);
// Get the current flags
$flags = $fruitsArrayObject->getFlags();
var_dump($flags);
// Set new flags
$fruitsArrayObject->setFlags(ArrayObject::ARRAY_AS_PROPS);
// Get the new flags
$flags = $fruitsArrayObject->getFlags();
var_dump($flags);
?>
以上例程会输出:
int(0)
int(2)
参见
- ArrayObject::setFlags
ArrayObject::getIterator
Create a new iterator from an ArrayObject instance
说明
public <span class="type">ArrayIterator <span class="methodname">ArrayObject::getIterator ( <span class="methodparam">void )
Create a new iterator from an ArrayObject instance.
参数
此函数没有参数。
返回值
An iterator from an ArrayObject.
范例
示例 #1 ArrayObject::getIterator example
<?php
$array = array('1' => 'one',
'2' => 'two',
'3' => 'three');
$arrayobject = new ArrayObject($array);
$iterator = $arrayobject->getIterator();
while($iterator->valid()) {
echo $iterator->key() . ' => ' . $iterator->current() . "\n";
$iterator->next();
}
?>
以上例程会输出:
1 => one
2 => two
3 => three
ArrayObject::getIteratorClass
Gets the iterator classname for the ArrayObject
说明
public string ArrayObject::getIteratorClass ( <span class="methodparam">void )
Gets the class name of the array iterator that is used by ArrayObject::getIterator().
参数
此函数没有参数。
返回值
Returns the iterator class name that is used to iterate over this object.
范例
示例 #1 ArrayObject::getIteratorClass example
<?php
// Custom ArrayIterator (inherits from ArrayIterator)
class MyArrayIterator extends ArrayIterator {
// custom implementation
}
// Array of available fruits
$fruits = array("lemons" => 1, "oranges" => 4, "bananas" => 5, "apples" => 10);
$fruitsArrayObject = new ArrayObject($fruits);
// Get the current class name
$className = $fruitsArrayObject->getIteratorClass();
var_dump($className);
// Set new classname
$fruitsArrayObject->setIteratorClass('MyArrayIterator');
// Get the new iterator classname
$className = $fruitsArrayObject->getIteratorClass();
var_dump($className);
?>
以上例程会输出:
string(13) "ArrayIterator"
string(15) "MyArrayIterator"
参见
- The ArrayObject::setIteratorClass method
ArrayObject::ksort
Sort the entries by key
说明
public void
ArrayObject::ksort ([ <span
class="methodparam">int $flags<span
class="initializer"> = SORT_REGULAR ] )
Sorts the entries by key, maintaining key to entry correlations. This is useful mainly for associative arrays.
参数
flags
The optional second parameter flags may be used to modify the sorting
behavior using these values:
Sorting type flags:
SORT_REGULAR- compare items normally; the details are described in the comparison operators sectionSORT_NUMERIC- compare items numericallySORT_STRING- compare items as strings-
SORT_LOCALE_STRING- compare items as strings, based on the current locale. It uses the locale, which can be changed using setlocale -
SORT_NATURAL- compare items as strings using "natural ordering" like natsort -
SORT_FLAG_CASE- can be combined (bitwise OR) withSORT_STRINGorSORT_NATURALto sort strings case-insensitively
返回值
没有返回值。
范例
示例 #1 ArrayObject::ksort example
<?php
$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
$fruitArrayObject = new ArrayObject($fruits);
$fruitArrayObject->ksort();
foreach ($fruitArrayObject as $key => $val) {
echo "$key = $val\n";
}
?>
以上例程会输出:
a = orange
b = banana
c = apple
d = lemon
参见
- ArrayObject::asort
- ArrayObject::natsort
- ArrayObject::natcasesort
- ArrayObject::uasort
- ArrayObject::uksort
ArrayObject::natcasesort
Sort an array using a case insensitive "natural order" algorithm
说明
public void ArrayObject::natcasesort ( <span class="methodparam">void )
This method is a case insensitive version of ArrayObject::natsort.
This method implements a sort algorithm that orders alphanumeric strings in the way a human being would while maintaining key/value associations. This is described as a "natural ordering".
参数
此函数没有参数。
返回值
没有返回值。
范例
示例 #1 ArrayObject::natcasesort example
<?php
$array = array('IMG0.png', 'img12.png', 'img10.png', 'img2.png', 'img1.png', 'IMG3.png');
$arr1 = new ArrayObject($array);
$arr2 = clone $arr1;
$arr1->asort();
echo "Standard sorting\n";
print_r($arr1);
$arr2->natcasesort();
echo "\nNatural order sorting (case-insensitive)\n";
print_r($arr2);
?>
以上例程会输出:
Standard sorting
ArrayObject Object
(
[0] => IMG0.png
[5] => IMG3.png
[4] => img1.png
[2] => img10.png
[1] => img12.png
[3] => img2.png
)
Natural order sorting (case-insensitive)
ArrayObject Object
(
[0] => IMG0.png
[4] => img1.png
[3] => img2.png
[5] => IMG3.png
[2] => img10.png
[1] => img12.png
)
For more information see: Martin Pool's » Natural Order String Comparison page.
参见
- ArrayObject::asort
- ArrayObject::ksort
- ArrayObject::natsort
- ArrayObject::uasort
- ArrayObject::uksort
ArrayObject::natsort
Sort entries using a "natural order" algorithm
说明
public void ArrayObject::natsort ( <span class="methodparam">void )
This method implements a sort algorithm that orders alphanumeric strings in the way a human being would while maintaining key/value associations. This is described as a "natural ordering". An example of the difference between this algorithm and the regular computer string sorting algorithms (used in ArrayObject::asort) method can be seen in the example below.
参数
此函数没有参数。
返回值
没有返回值。
范例
示例 #1 ArrayObject::natsort example
<?php
$array = array("img12.png", "img10.png", "img2.png", "img1.png");
$arr1 = new ArrayObject($array);
$arr2 = clone $arr1;
$arr1->asort();
echo "Standard sorting\n";
print_r($arr1);
$arr2->natsort();
echo "\nNatural order sorting\n";
print_r($arr2);
?>
以上例程会输出:
Standard sorting
ArrayObject Object
(
[3] => img1.png
[1] => img10.png
[0] => img12.png
[2] => img2.png
)
Natural order sorting
ArrayObject Object
(
[3] => img1.png
[2] => img2.png
[1] => img10.png
[0] => img12.png
)
For more information see: Martin Pool's » Natural Order String Comparison page.
参见
- ArrayObject::asort
- ArrayObject::ksort
- ArrayObject::natcasesort
- ArrayObject::uasort
- ArrayObject::uksort
ArrayObject::offsetExists
Returns whether the requested index exists
说明
public bool
ArrayObject::offsetExists ( <span
class="methodparam">mixed $index )
参数
index
The index being checked.
返回值
true if the requested index exists, otherwise false
范例
示例 #1 ArrayObject::offsetExists example
<?php
$arrayobj = new ArrayObject(array('zero', 'one', 'example'=>'e.g.'));
var_dump($arrayobj->offsetExists(1));
var_dump($arrayobj->offsetExists('example'));
var_dump($arrayobj->offsetExists('notfound'));
?>
以上例程会输出:
bool(true)
bool(true)
bool(false)
ArrayObject::offsetGet
Returns the value at the specified index
说明
public mixed
ArrayObject::offsetGet ( <span
class="methodparam">mixed $index )
参数
index
The index with the value.
返回值
The value at the specified index or null.
错误/异常
Produces an E_NOTICE error message when the specified index does
not exist.
范例
示例 #1 ArrayObject::offsetGet example
<?php
$arrayobj = new ArrayObject(array('zero', 7, 'example'=>'e.g.'));
var_dump($arrayobj->offsetGet(1));
var_dump($arrayobj->offsetGet('example'));
var_dump($arrayobj->offsetExists('notfound'));
?>
以上例程会输出:
int(7)
string(4) "e.g."
bool(false)
ArrayObject::offsetSet
为指定索引设定新的值
说明
public void
ArrayObject::offsetSet ( <span
class="methodparam">mixed $index ,
mixed
$newval )
设置指定的索引为新的值
参数
index
将要被设置的索引
newval
参数 index 所对应的新值。
返回值
没有返回值。
范例
示例 #1 ArrayObject::offsetSet 例子
<?php
class Example {
public $property = 'prop:public';
}
$arrayobj = new ArrayObject(new Example());
$arrayobj->offsetSet(4, 'four');
$arrayobj->offsetSet('group', array('g1', 'g2'));
var_dump($arrayobj);
$arrayobj = new ArrayObject(array('zero','one'));
$arrayobj->offsetSet(null, 'last');
var_dump($arrayobj);
?>
以上例程会输出:
object(ArrayObject)#1 (3) {
["property"]=>
string(11) "prop:public"
[4]=>
string(4) "four"
["group"]=>
array(2) {
[0]=>
string(2) "g1"
[1]=>
string(2) "g2"
}
}
object(ArrayObject)#3 (3) {
[0]=>
string(4) "zero"
[1]=>
string(3) "one"
[2]=>
string(4) "last"
}
参见
- ArrayObject::append
ArrayObject::offsetUnset
Unsets the value at the specified index
说明
public void
ArrayObject::offsetUnset ( <span
class="methodparam">mixed $index )
Unsets the value at the specified index.
参数
index
The index being unset.
返回值
没有返回值。
范例
示例 #1 ArrayObject::offsetUnset example
<?php
$arrayobj = new ArrayObject(array(0=>'zero',2=>'two'));
$arrayobj->offsetUnset(2);
var_dump($arrayobj);
?>
以上例程会输出:
object(ArrayObject)#1 (1) {
[0]=>
string(4) "zero"
}
ArrayObject::serialize
Serialize an ArrayObject
说明
public string ArrayObject::serialize ( <span class="methodparam">void )
Serializes an ArrayObject.
Warning
本函数还未编写文档,仅有参数列表。
参数
此函数没有参数。
返回值
The serialized representation of the <span class="classname">ArrayObject.
范例
示例 #1 ArrayObject::serialize example
<?php
$o = new ArrayObject();
$s1 = serialize($o);
$s2 = $o->serialize();
var_dump($s1);
var_dump($s2);
?>
以上例程会输出:
string(45) "C:11:"ArrayObject":21:{x:i:0;a:0:{};m:a:0:{}}"
string(21) "x:i:0;a:0:{};m:a:0:{}"
参见
- ArrayObject::unserialize
- serialize
- Serializing Objects
ArrayObject::setFlags
Sets the behavior flags
说明
public void
ArrayObject::setFlags ( <span
class="methodparam">int $flags )
Set the flags that change the behavior of the ArrayObject.
参数
flags
The new ArrayObject behavior. It takes on either a bitmask, or named
constants. Using named constants is strongly encouraged to ensure
compatibility for future versions.
The available behavior flags are listed below. The actual meanings of these flags are described in the predefined constants.
| value | constant |
|---|---|
| 1 | ArrayObject::STD_PROP_LIST |
| 2 | ArrayObject::ARRAY_AS_PROPS |
返回值
没有返回值。
范例
示例 #1 ArrayObject::setFlags example
<?php
// Array of available fruits
$fruits = array("lemons" => 1, "oranges" => 4, "bananas" => 5, "apples" => 10);
$fruitsArrayObject = new ArrayObject($fruits);
// Try to use array key as property
var_dump($fruitsArrayObject->lemons);
// Set the flag so that the array keys can be used as properties of the ArrayObject
$fruitsArrayObject->setFlags(ArrayObject::ARRAY_AS_PROPS);
// Try it again
var_dump($fruitsArrayObject->lemons);
?>
以上例程会输出:
NULL
int(1)
ArrayObject::setIteratorClass
Sets the iterator classname for the ArrayObject
说明
public void
ArrayObject::setIteratorClass ( <span
class="methodparam">string
$iterator_class )
Sets the classname of the array iterator that is used by ArrayObject::getIterator().
参数
iterator_class
The classname of the array iterator to use when iterating over this
object.
返回值
没有返回值。
范例
示例 #1 ArrayObject::setIteratorClass example
<?php
// Custom ArrayIterator (inherits from ArrayIterator)
class MyArrayIterator extends ArrayIterator {
// custom implementation
}
// Array of available fruits
$fruits = array("lemons" => 1, "oranges" => 4, "bananas" => 5, "apples" => 10);
$fruitsArrayObject = new ArrayObject($fruits);
// Set the iterator classname to the newly
$fruitsArrayObject->setIteratorClass('MyArrayIterator');
print_r($fruitsArrayObject->getIterator());
?>
以上例程会输出:
MyArrayIterator Object
(
[lemons] => 1
[oranges] => 4
[bananas] => 5
[apples] => 10
)
ArrayObject::uasort
Sort the entries with a user-defined comparison function and maintain key association
说明
public void
ArrayObject::uasort ( <span
class="methodparam">callable
$cmp_function )
This function sorts the entries such that keys maintain their correlation with the entry that they are associated with, using a user-defined comparison function.
This is used mainly when sorting associative arrays where the actual element order is significant.
参数
cmp_function
Function cmp_function should accept two parameters which will be
filled by pairs of entries. 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.
返回值
没有返回值。
范例
示例 #1 ArrayObject::uasort example
<?php
// Comparison function
function cmp($a, $b) {
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
// Array to be sorted
$array = array('a' => 4, 'b' => 8, 'c' => -1, 'd' => -9, 'e' => 2, 'f' => 5, 'g' => 3, 'h' => -4);
$arrayObject = new ArrayObject($array);
print_r($arrayObject);
// Sort and print the resulting array
$arrayObject->uasort('cmp');
print_r($arrayObject);
?>
以上例程会输出:
Array
(
[a] => 4
[b] => 8
[c] => -1
[d] => -9
[e] => 2
[f] => 5
[g] => 3
[h] => -4
)
Array
(
[d] => -9
[h] => -4
[c] => -1
[e] => 2
[g] => 3
[a] => 4
[f] => 5
[b] => 8
)
参见
- ArrayObject::asort
- ArrayObject::ksort
- ArrayObject::natsort
- ArrayObject::natcasesort
- ArrayObject::uksort
ArrayObject::uksort
Sort the entries by keys using a user-defined comparison function
说明
public void
ArrayObject::uksort ( <span
class="methodparam">callable
$cmp_function )
This function sorts the keys of the entries using a user-supplied comparison function. The key to entry correlations will be maintained.
参数
cmp_function
The callback comparison function.
Function cmp_function should accept two parameters which will be
filled by pairs of entry keys. 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.
返回值
没有返回值。
范例
示例 #1 ArrayObject::uksort example
<?php
function cmp($a, $b) {
$a = preg_replace('@^(a|an|the) @', '', $a);
$b = preg_replace('@^(a|an|the) @', '', $b);
return strcasecmp($a, $b);
}
$array = array("John" => 1, "the Earth" => 2, "an apple" => 3, "a banana" => 4);
$arrayObject = new ArrayObject($array);
$arrayObject->uksort('cmp');
foreach ($arrayObject as $key => $value) {
echo "$key: $value\n";
}
?>
以上例程会输出:
an apple: 3
a banana: 4
the Earth: 2
John: 1
参见
- ArrayObject::asort
- ArrayObject::ksort
- ArrayObject::natsort
- ArrayObject::natcasesort
- ArrayObject::uasort
ArrayObject::unserialize
Unserialize an ArrayObject
说明
public void
ArrayObject::unserialize ( <span
class="methodparam">string
$serialized )
Unserializes a serialized ArrayObject.
Warning
本函数还未编写文档,仅有参数列表。
参数
serialized
The serialized ArrayObject.
返回值
The unserialized ArrayObject.
参见
- ArrayIterator::serialize
- unserialize
- Serializing Objects
简介
The SplObserver interface is used alongside SplSubject to implement the Observer Design Pattern.
接口摘要
SplObserver
class SplObserver {
/* 方法 */
abstract <span
class="modifier">public void <span
class="methodname">update ( <span
class="type">SplSubject $subject )
}
SplObserver::update
Receive update from subject
说明
abstract <span
class="modifier">public void <span
class="methodname">SplObserver::update ( <span
class="methodparam">SplSubject
$subject )
This method is called when any SplSubject to which the observer is attached calls <span class="methodname">SplSubject::notify.
Warning
本函数还未编写文档,仅有参数列表。
参数
subject
The SplSubject notifying the observer of
an update.
返回值
没有返回值。
简介
The SplSubject interface is used alongside SplObserver to implement the Observer Design Pattern.
接口摘要
SplSubject
class SplSubject {
/* 方法 */
abstract <span
class="modifier">public void <span
class="methodname">attach ( <span
class="type">SplObserver $observer )
abstract <span
class="modifier">public void <span
class="methodname">detach ( <span
class="type">SplObserver $observer )
abstract <span class="modifier">public void <span class="methodname">notify ( void )
}
SplSubject::attach
Attach an SplObserver
说明
abstract <span
class="modifier">public void <span
class="methodname">SplSubject::attach ( <span
class="methodparam">SplObserver
$observer )
Attaches an SplObserver so that it can be notified of updates.
Warning
本函数还未编写文档,仅有参数列表。
参数
observer
The SplObserver to attach.
返回值
没有返回值。
SplSubject::detach
Detach an observer
说明
abstract <span
class="modifier">public void <span
class="methodname">SplSubject::detach ( <span
class="methodparam">SplObserver
$observer )
Detaches an observer from the subject to no longer notify it of updates.
Warning
本函数还未编写文档,仅有参数列表。
参数
observer
The SplObserver to detach.
返回值
没有返回值。
SplSubject::notify
Notify an observer
说明
abstract <span class="modifier">public void <span class="methodname">SplSubject::notify ( <span class="methodparam">void )
Notifies all attached observers.
Warning
本函数还未编写文档,仅有参数列表。
参数
此函数没有参数。
返回值
没有返回值。