0% found this document useful (0 votes)
35 views

PHP Sorting Arrays

This document discusses various ways to sort arrays in PHP using functions like sort(), rsort(), asort(), ksort(), arsort(), and krsort(). It also explains PHP superglobal variables that provide information about headers, paths, scripts, forms, and more, including $GLOBALS, $_SERVER, $_REQUEST, $_POST, $_GET, and how to access submitted form data with them. Common PHP sorting and global variables are demonstrated through examples.

Uploaded by

Car Lo
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

PHP Sorting Arrays

This document discusses various ways to sort arrays in PHP using functions like sort(), rsort(), asort(), ksort(), arsort(), and krsort(). It also explains PHP superglobal variables that provide information about headers, paths, scripts, forms, and more, including $GLOBALS, $_SERVER, $_REQUEST, $_POST, $_GET, and how to access submitted form data with them. Common PHP sorting and global variables are demonstrated through examples.

Uploaded by

Car Lo
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

PHP Sorting Arrays

PHP - Sort
• The elements in an array can be sorted in alphabetical or numerical order,
descending or ascending

The following are PHP array sort functions:


• sort() - sort arrays in ascending order
• rsort() - sort arrays in descending order
• asort() - sort associative arrays in ascending order, according to the value
• ksort() - sort associative arrays in ascending order, according to the key
• arsort() - sort associative arrays in descending order, according to the value
• krsort() - sort associative arrays in descending order, according to the key
Sort Array in Ascending Order - sort()
• The following example sorts the elements of the $cars array in
ascending alphabetical order:

Example:
• <?php
• $cars = array("Volvo", "BMW", "Toyota");
• sort($cars);
• ?>
Sort Array in Ascending Order - sort()
• The following example sorts the elements of the $numbers array in
ascending numerical order:

Example:
<?php
$numbers = array(4, 6, 2, 22, 11);
sort($numbers);
?>
Sort Array in Descending Order - rsort()
• The following example sorts the elements of the $cars array in
descending alphabetical order:

Example
<?php
$cars = array("Volvo", "BMW", "Toyota");
rsort($cars);
?>
Sort Array in Descending Order - rsort()
• The following example sorts the elements of the $numbers array in
descending numerical order:

Example
<?php
$numbers = array(4, 6, 2, 22, 11);
rsort($numbers);
?>
Sort Array (Ascending Order), According to
Value - asort()
• The following example sorts an associative array in ascending order, according to the
value:
Example
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
asort($age);
foreach($age as $x => $x_value) {
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
?>
Sort Array (Ascending Order), According to
Key - ksort()
• The following example sorts an associative array in ascending order,
according to the key:

Example
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
ksort($age);
?>
Sort Array (Descending Order), According to
Value - arsort()
• The following example sorts an associative array in descending order,
according to the value:

Example
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
arsort($age);
?>
Sort Array (Descending Order), According to
Key - krsort()
• The following example sorts an associative array in descending order,
according to the key:

Example
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
krsort($age);
?>
PHP Global Variables -
Superglobals
PHP Global Variables - Superglobals
• Several predefined variables in PHP are "superglobals", which means
that they are always accessible, regardless of scope - and you can
access them from any function, class or file without having to do
anything special.

The PHP superglobal variables are:


$GLOBALS $_SERVER $_REQUEST $_POST
$_GET $_FILES $_ENV $_COOKIE
$_SESSION
PHP $GLOBALS
• $GLOBALS is a PHP super global variable which is used to access global variables
from anywhere in the PHP script (also from within functions or methods).
• PHP stores all global variables in an array called $GLOBALS[index]. The index holds
the name of the variable. Example:
<?php
$x = 75;
$y = 25;
function addition() {
$GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y']; }
addition();
echo $z;
?>
PHP $_SERVER
• $_SERVER is a PHP super global variable which holds information about headers,
paths, and script locations.
Example
<?php
echo $_SERVER['PHP_SELF'];
echo "<br>";
echo $_SERVER['SERVER_NAME'];
echo "<br>";
echo $_SERVER['HTTP_HOST'];
echo "<br>";
echo $_SERVER['SCRIPT_NAME'];
?>
The following table lists the most important
elements that can go inside $_SERVER:
PHP $_REQUEST
• PHP $_REQUEST is used to collect data after submitting an HTML
form.
• The example below shows a form with an input field and a submit
button. When a user submits the data by clicking on "Submit", the
form data is sent to the file specified in the action attribute of the
<form> tag.
• In this example, we point to this file itself for processing form data. If
you wish to use another PHP file to process form data, replace that
with the filename of your choice. Then, we can use the super global
variable $_REQUEST to collect the value of the input field:
Example: PHP $_REQUEST
<html> <body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Name: <input type="text" name="fname">
<input type="submit"> </form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_REQUEST['fname'];
if (empty($name)) { echo "Name is empty";
} else { echo $name; } } ?>
</body> </html>
PHP $_POST
• PHP $_POST is widely used to collect form data after submitting an
HTML form with method="post". $_POST is also widely used to pass
variables.
• The example below shows a form with an input field and a submit
button. When a user submits the data by clicking on "Submit", the form
data is sent to the file specified in the action attribute of the <form> tag.
• In this example, we point to the file itself for processing form data. If
you wish to use another PHP file to process form data, replace that with
the filename of your choice. Then, we can use the super global variable
$_POST to collect the value of the input field:
Example: PHP $_POST
<html> <body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Name: <input type="text" name="fname">
<input type="submit"> </form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST['fname'];
if (empty($name)) { echo "Name is empty";
} else { echo $name; } } ?>
</body> </html>
PHP $_GET
• PHP $_GET can also be used to collect form data after submitting an HTML
form with method="get".
• $_GET can also collect data sent in the URL. Assume we have an HTML page
that contains a hyperlink with parameters: "test_view.php"
<html>
<body>
<a href="test_get.php?subject= PHP & Web = W3schools.com">Test $GET method</a>
</body>
</html>
When a user clicks on the link "Test $GET", the parameters "subject" and "web" are sent
to "test_get.php", and you can then access their values in "test_get.php" with $_GET.
PHP $_GET
The example below shows the code in "test_get.php":
<html>
<body>
<?php
echo "Study " . $_GET['subject'] . " at " . $_GET['web'];
?>
</body>
</html>

You might also like