Professional Documents
Culture Documents
home |
O'Reilly's CD bookshelfs |
FreeBSD | Linux | Cisco |
Cisco Exam
5.8. Sorting
Ads by
Sorting
changes the internal order of elements in an array and optionally
rewrites the keys to reflect this new order. For
example, you might
use sorting to arrange a list of scores from biggest to smallest, to
alphabetize a list of names, or to order a
Send feedba
set of users based on how
many messages they posted.
Why this ad?
PHP provides three ways to sort arrays—sorting by keys, sorting
by values without changing the keys, or sorting by values Tech
and then
changing the keys. Each kind of sort can be done in ascending order,
descending order, or an order defined by a
user-defined function. interview
5.8.1. Sorting One Array at a Time are hard
Akudemy -
The
functions provided by PHP to sort
an array are shown in Table 5-1. Interview
Coaching
Ads by
Table 5-1. PHP functions for sorting an array Learn what you
need to pass To
tech exams, sta
Send feedback
today!
Effect Ascending Descending User-defined order
Why this ad?
Tech
github.com
interviews
Sort array by values, then reassign indexes starting with 0 sort( ) rsort( ) usort( )
are hard.
Akudemy -
Sort array by values asort( ) arsort( ) uasort( )
Interview
Coaching
Learn what you
Sort array by keys ksort( ) krsort( ) uksort( ) OPEN
need to pass Top
today! The sort( ), rsort( ), and usort( ) functions are designed to work on
indexed arrays, because they
assign new numeric
keys to represent the ordering.
They're useful when you need to answer questions
like "what are the top 10 scores?"
and
github.com
"who's the third person in
alphabetical order?" The other sort functions can be
used on indexed arrays, but you'll only be able
to
access the sorted ordering by using traversal functions such as
foreach and next.
ksort($logins);
echo("<table>\n");
foreach ($logins as $user => $time) {
https://docstore.mik.ua/orelly/webprog/php/ch05_08.htm 1/4
9/1/2021 Sorting (Programming PHP)
echo("<tr><td>$user</td><td>$time</td></tr>\n");
}
echo("</table>\n");
<table>
<tr><td>jht</td><td>441</td></tr>
<tr><td>jj</td><td>441</td></tr>
<tr><td>kt</td><td>492</td></tr>
<tr><td>njt</td><td>415</td></tr>
<tr><td>rl</td><td>652</td></tr>
<tr><td>wt</td><td>402</td></tr>
</table>
User-defined ordering
requires that you provide a function that takes two values and
returns a value that specifies the order of
the two values in the
sorted array. The function should return 1 if the first value is
greater than the second, -1 if the first value
is less than the
second, and 0 if the values are the same for the purposes of your
custom sort order.
<?php
function user_sort($a, $b) {
// smarts is all-important, so sort it first
if($b == 'smarts') {
return 1;
}
Ads by else if($a == 'smarts') {
return -1;
}
Send feedback
return ($a == $b) ? 0 : (($a < $b) ? -1 : 1);
Why this ad? }
Tech
Akudemy -
if($submitted) {
Interview
if($sort_type == 'usort' || $sort_type == 'uksort' || $sort_type == 'uasort') {
$sort_type($values, 'user_sort');
Coaching
}
Learn what you
else {
need to pass Top
$sort_type($values);
tech exams, start
}
}
today!
?>
github.com
<form action="index.php">
<p>
<input type="radio" name="sort_type" value="sort" checked="checked" />
Standard sort<br />
<input type="radio" name="sort_type" value="rsort" /> Reverse sort<br />
<input type="radio" name="sort_type" value="usort" /> User-defined sort<br />
<input type="radio" name="sort_type" value="ksort" /> Key sort<br />
<input type="radio" name="sort_type" value="krsort" /> Reverse key sort<br />
<input type="radio" name="sort_type" value="uksort" /> User-defined key sort<br />
<input type="radio" name="sort_type" value="asort" /> Value sort<br />
OPEN <input type="radio" name="sort_type" value="arsort" /> Reverse value sort<br />
<input type="radio" name="sort_type" value="uasort" /> User-defined value sort<br />
</p>
<p align="center">
<input type="submit" value="Sort" name="submitted" />
</p>
<p>
Values <?= $submitted ? "sorted by $sort_type" : "unsorted"; ?>:
</p>
<ul>
<?php
foreach($values as $key=>$value) {
echo "<li><b>$key</b>: $value</li>";
}
?>
</ul>
</form>
PHP's
built-in sort functions correctly sort strings and numbers, but they
don't correctly sort strings that contain numbers.
For
Ads byand ex1.php, the
normal sort functions will rearrange them in this
example, if you have the filenames ex10.php,
ex5.php,
order:
ex1.php, ex10.php,
ex5.php. ToSendcorrectly sort stringsWhy
feedback that contain
numbers,
this ad? use the natsort(
) and natcasesort( )
functions:
$output = natsort(input);
$output = natcasesort(input);
https://docstore.mik.ua/orelly/webprog/php/ch05_08.htm 2/4
9/1/2021 Sorting (Programming PHP)
5.8.3. Sorting Multiple Arrays at Once
The array_multisort(
)
function sorts multiple indexed
arrays at once:
Imagine that you have a lot of people, and several pieces of data on
each person:
interviews
echo("</table>\n");
<table>
<tr><td>35</td><td>64141</td><td>Brenda</td>
Interview
<tr><td>35</td><td>02140</td><td>Dick</td>
Coaching </table>
Learn what you
$flipped = array_flip(array);
OPEN That is, for each element of the array whose value is a valid key,
the element's value becomes its key and the
element's key
becomes its value. For example, if you
have an array mapping usernames to home directories, you can use
array_flip( )
to create an array mapping home
directories to usernames:
Elements whose original values are neither strings nor integers are
left alone in the resulting array. The new array lets you
discover
the key in the original array given its value, but this technique
works effectively only when the original array has
unique values.
To traverse the
elements in an
array in a random order, use the shuffle( )
function. All existing keys, whether string or
numeric, are replaced
with consecutive integers starting at 0.
https://docstore.mik.ua/orelly/webprog/php/ch05_08.htm 3/4
9/1/2021 Sorting (Programming PHP)
[2] => Monday
[3] => Friday
[4] => Wednesday
[5] => Saturday
[6] => Sunday
)
Obviously, the order after your shuffle( ) may not be the same as the
sample output here. Unless you are interested in
getting multiple
random elements from an array, without repeating any specific item,
using the rand( ) function to pick an
index is more
efficient.
Ads by
Ads by
Send feedback
Send feedback Why this ad?
Why this ad?
Achieve the job of your dreams
Tech
Looking for a JavaScript job?
interviews
High pay, flexibility, safety, and satisfaction, often mean scary tech interviews.
are hard.
Akudemy -
Interview
today!
github.com
OPEN
Ads by
Send feedback Why this ad?
https://docstore.mik.ua/orelly/webprog/php/ch05_08.htm 4/4