settingsAccountsettings
By using our mini forum, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy
Menusettings

Q: Arrays - functions and sorting in PHP

+9 votes

Can you please share the advanced functionality in PHP of the arrays and the associative arrays and their functions and sorting functions?

Please give some examples with code.

Thanks

 

asked in PHP category by user eiorgert
edited by user golearnweb

3 Answers

+8 votes

Some commonly used:

array functions php

Click here to see all of them

Examples:

array_push($arr, variable) - push (add) one or more elements into the end of array:

$arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
array_push($arr, 11, 12, 13, 100);

echo implode(", ", $arr);
// $arr = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 100

array_pop($arr) - Pop (remove) the element of the end of the array:

$arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
array_pop($arr);

echo implode(", ", $arr);
// $arr = 1, 2, 3, 4, 5, 6, 7, 8, 9


array_unshift($arr, variable) - Prepend one or more elements to the beginning of an array:

$arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
array_unshift($arr, -1, 0);

echo implode(", ", $arr);
// $arr = -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

array_shift($arr) – Shift (remove) an element off the beginning of array:

$arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
array_shift($arr);

echo implode(", ", $arr);
// $arr = 2, 3, 4, 5, 6, 7, 8, 9, 10

array_splice($arr, offset, length, replacement): Removes a portion of the array and replace it with something else and return removed elements:

Remove functionality:

$arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
array_splice($arr, 8, 2); //Removes 9 and 10

echo implode(", ", $arr);
// $arr = 1, 2, 3, 4, 5, 6, 7, 8

Replace functionality:

$arr = [1, 2, 3, 4, 5, 6, 7, 8];
array_splice($arr, 3, 2, 3.3);

echo implode(", ", $arr);
// $arr = 1, 2, 3, 3.3, 6, 7, 8

array_unique($arr) - Removes duplicate values from an array:

$names = ["Peter", "George", "Toni", "George", "Peter", "Gabriel"];
$names = array_unique($names);

echo implode(", ", $names);
//$names = Peter, George, Toni, Gabriel

array_reverse($arr) - Returns an array in reverse order:

$arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
$arr = array_reverse($arr);

echo implode(", ", $arr);
//$arr = 10, 9, 8, 7, 6, 5, 4, 3, 2, 1

array_sum($arr) - Calculate the sum of values in an array:

$arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
$sum = array_sum($arr);

echo $sum;
//$sum =55

array_filter($arr) - Removes empty array elements or values + also removes null, false, 0 (zero) values:

$arr = explode(" ", "Hi, my    name is      Peter");
var_dump($arr);
// A lot of empty spaces in $arr (13 elements)

$arr = array_filter($arr);

var_dump($arr);
//$arr (5 elements)

array_search(searchValue, array) - Searches for a given value and returns the first corresponding key if successful. Searches for needle in haystack:

$arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

$el = array_search(9, $arr);
echo $el;
//$el = 8

in_array(searchValue, $arr, true) - Checks if a value exists in an array (3rd parameter is optional). Check the types of searchValue. Like array_search but returns boolean: true or false:

$arr = [1, 2, 3, '4', 5, 6, 7, 8, 9, 10];

var_dump($isContain = in_array(10, $arr));
// true or 1

var_dump($isContain = in_array(22, $arr));
// false or 0 or empty string

var_dump($isContain = in_array('4', $arr, true));
// true or 1

var_dump($isContain = in_array(4, $arr, true));
// false or 0 or empty string

Sorting Array:

sort($arr) - Sort in natural (ascending) order:

$names = ["George", "Allan", "Peter"];
sort($names);

var_dump($names);
//$names = ["Allan", "George", "Peter"]

rsort($arr) - Sort in reverse (descending) order:

$names = ["George", "Allan", "Peter"];
rsort($names);

var_dump($names);
//$names = ["Peter", "George", "Allan"]

 

answered by user mitko
edited by user mitko
+3 votes

Fo understanding the array_splice() function in Php see this task: https://forum.tutorials7.com/2510/change-array-php-array-task

Type 1 to remove and 0 to replace :-)

answered by user Jolie Ann
+2 votes

Functions for sorting associative arrays in PHP:

KEYS sorting:

ksort() - Sort an array by KEY in ascending order:

<?php

$ages = ["Peter" => 37, "Ben" => 35, "Joe" => 43];

ksort($ages);
print_r($ages);
//Ben => 35, Joe => 43, Peter => 37

krsort() - Sort an array by KEY in descending order:

<?php

$ages = ["Peter" => 37, "Ben" => 35, "Joe" => 43];

krsort($ages);
print_r($ages);
//Peter => 37, Joe => 43, Ben => 35

uksort() - Sort an array by KEYS using a user-defined function:


VALUES Sorting:

asort() - Sort an array by VALUE in ascending order:

<?php

$ages = ["Peter" => 37, "Ben" => 35, "Joe" => 43];

asort($ages);
print_r($ages);
//Ben => 35, Peter => 37, Joe => 43

arsort() - Sort an array by VALUE in descending order:

<?php

$ages = ["Peter" => 37, "Ben" => 35, "Joe" => 43];

arsort($ages);
print_r($ages);
//Joe => 43, Peter => 37, Ben => 35

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


For sorting you can also use Spaceship Comparison Operator <=>:

<=> - An integer less than, equal to, or greater than zero when first value is respectively less than, equal to, or greater than second value

The spaceship operator <=> returns:

  • -1 if the left side is smaller
  • 0 if the values are equal
  • 1 if the left side is larger

It can be used on all generic PHP values with the same semantics as < , <=, ==, >=, >. This operator can be used with integers, floats, strings, arrays, objects, etc:

// Integers
echo 1 <=> 1; // 0
echo 1 <=> 2; // -1
echo 2 <=> 1; // 1
  
// Floats
echo 1.5 <=> 1.5; // 0
echo 1.5 <=> 2.5; // -1
echo 2.5 <=> 1.5; // 1
  
// Strings
echo "a" <=> "a"; // 0
echo "a" <=> "b"; // -1
echo "b" <=> "a"; // 1
  
echo "a" <=> "aa"; // -1
echo "zz" <=> "aa"; // 1
  
// Arrays
echo [] <=> []; // 0
echo [1, 2, 3] <=> [1, 2, 3]; // 0
echo [1, 2, 3] <=> []; // 1
echo [1, 2, 3] <=> [1, 2, 1]; // 1
echo [1, 2, 3] <=> [1, 2, 4]; // -1

sorting function attributes

answered by user golearnweb
edited by user golearnweb
...