find the smallest element in an array in php

To find the smallest element in an array in PHP, you can first sort the array in ascending order using the sort() function, which will arrange the elements in the array in increasing order.

Then, you can access the first element of the array, which is the smallest element in the array. Here's an example:

main.php
<?php
// define an array
$numbers = array(3, 6, 2, 8, 1, 9);

// sort the array in ascending order
sort($numbers);

// access the first element of the array, which is the smallest
$smallest = $numbers[0];

// output the smallest element
echo "The smallest element in the array is: " . $smallest;
?>
299 chars
14 lines

Output:

main.php
The smallest element in the array is: 1
40 chars
2 lines

related categories

gistlibby LogSnag