Function Reference

Any problem with PHP can be disscused here
moslemir
Posts: 21
Joined: Sun Mar 18, 2007 12:29 pm
Contact:

Post by moslemir »

sizeof

(PHP 4, PHP 5)

sizeof — Alias of count()

Description
This function is an alias of: count().


--------------------------------------------------------------------------
sort

(PHP 4, PHP 5)

sort — Sort an array

Description
bool sort ( array &$array [, int $sort_flags] )

This function sorts an array. Elements will be arranged from lowest to highest when this function has completed.

Note: This function assigns new keys for the elements in array. It will remove any existing keys you may have assigned, rather than just reordering the keys.

Returns TRUE on success or FALSE on failure.


Example

[PHP]<?php

$fruits = array("lemon", "orange", "banana", "apple");
sort($fruits);
foreach ($fruits as $key => $val) {
echo "fruits[" . $key . "] = " . $val . "\n";
}

?> [/PHP]
The above example will output:


fruits[0] = apple
fruits[1] = banana
fruits[2] = lemon
fruits[3] = orange





The fruits have been sorted in alphabetical order.

The optional second parameter sort_flags may be used to modify the sorting behavior using these values:

Sorting type flags:

SORT_REGULAR - compare items normally (don't change types)
SORT_NUMERIC - compare items numerically
SORT_STRING - compare items as strings
SORT_LOCALE_STRING - compare items as strings, based on the current locale. Added in PHP 4.4.0 and 5.0.2. Before PHP 6, it uses the system locale, which can be changed using setlocale(). Since PHP 6, you must use the i18n_loc_set_default() function.

Note: The second parameter was added in PHP 4.

Warning
Be careful when sorting arrays with mixed types values because sort() can produce unpredictable results.


See also arsort(), asort(), ksort(), krsort(), natsort(), natcasesort(), rsort(), usort(), array_multisort(), and uksort().


--------------------------------------------------------------------------
uasort

(PHP 4, PHP 5)

uasort — Sort an array with a user-defined comparison function and maintain index association

Description
bool uasort ( array &$array, callback $cmp_function )

This function sorts an array such that array indices maintain their correlation with the array elements they are associated with. This is used mainly when sorting associative arrays where the actual element order is significant. The comparison function is user-defined.

Returns TRUE on success or FALSE on failure.

Note: Please see usort() and uksort() for examples of user-defined comparison functions.

See also usort(), uksort(), sort(), asort(), arsort(), ksort(), and rsort().


--------------------------------------------------------------------------
uksort

(PHP 4, PHP 5)

uksort — Sort an array by keys using a user-defined comparison function

Description
bool uksort ( array &$array, callback $cmp_function )

uksort() will sort the keys of an array using a user-supplied comparison function. If the array you wish to sort needs to be sorted by some non-trivial criteria, you should use this function.

Function cmp_function should accept two parameters which will be filled by pairs of array 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.

Returns TRUE on success or FALSE on failure.


Example

[PHP]<?php
function cmp($a, $b)
{
$a = ereg_replace('^(a|an|the) ', '', $a);
$b = ereg_replace('^(a|an|the) ', '', $b);
return strcasecmp($a, $b);
}

$a = array("John" => 1, "the Earth" => 2, "an apple" => 3, "a banana" => 4);

uksort($a, "cmp");

foreach ($a as $key => $value) {
echo "$key: $value\n";
}
?> [/PHP]
The above example will output:


an apple: 3
a banana: 4
the Earth: 2
John: 1





See also usort(), uasort(), sort(), asort(), arsort(), ksort(), natsort(), and rsort().


--------------------------------------------------------------------------
usort

(PHP 4, PHP 5)

usort — Sort an array by values using a user-defined comparison function

Description
bool usort ( array &$array, callback $cmp_function )

This function will sort an array by its values using a user-supplied comparison function. If the array you wish to sort needs to be sorted by some non-trivial criteria, you should use this function.

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.


Note: If two members compare as equal, their order in the sorted array is undefined. Up to PHP 4.0.6 the user defined functions would keep the original order for those elements, but with the new sort algorithm introduced with 4.1.0 this is no longer the case as there is no solution to do so in an efficient way.


Note: This function assigns new keys for the elements in array. It will remove any existing keys you may have assigned, rather than just reordering the keys.

Returns TRUE on success or FALSE on failure.


Example

[PHP]<?php
function cmp($a, $b)
{
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}

$a = array(3, 2, 5, 6, 1);

usort($a, "cmp");

foreach ($a as $key => $value) {
echo "$key: $value\n";
}
?> [/PHP]
The above example will output:


0: 1
1: 2
2: 3
3: 5
4: 6





Note: Obviously in this trivial case the sort() function would be more appropriate.


Example. usort() example using multi-dimensional array

[PHP]<?php
function cmp($a, $b)
{
return strcmp($a["fruit"], $b["fruit"]);
}

$fruits[0]["fruit"] = "lemons";
$fruits[1]["fruit"] = "apples";
$fruits[2]["fruit"] = "****";

usort($fruits, "cmp");

while (list($key, $value) = each($fruits)) {
echo "\$fruits[$key]: " . $value["fruit"] . "\n";
}
?> [/PHP]
When sorting a multi-dimensional array, $a and $b contain references to the first index of the array.

The above example will output:


$fruits[0]: apples
$fruits[1]: ****
$fruits[2]: lemons






Example. usort() example using a member function of an object

[PHP]<?php
class TestObj {
var $name;

function TestObj($name)
{
$this->name = $name;
}

/* This is the static comparing function: */
function cmp_obj($a, $b)
{
$al = strtolower($a->name);
$bl = strtolower($b->name);
if ($al == $bl) {
return 0;
}
return ($al > $bl) ? +1 : -1;
}
}

$a[] = new TestObj("c");
$a[] = new TestObj("b");
$a[] = new TestObj("d");

usort($a, array("TestObj", "cmp_obj"));

foreach ($a as $item) {
echo $item->name . "\n";
}
?> [/PHP]
The above example will output:


b
c
d





See also uasort(), uksort(), sort(), asort(), arsort(),ksort(), natsort(), and rsort().


----------------------------------------------------
----------------------------
Lixas
Posts: 750
Joined: Wed Feb 16, 2005 4:21 pm

Post by Lixas »

for God, no need to copy!!!

-=Locked=-
Image
Locked