Basic functions for working with arrays in PHP. Example of u... open

Basic functions for working with arrays in PHP. Example of use

Approved. Code works!
This is exactly the working code that is verified by the moderator or site administrators
Tested: PHP 5.6 +

A list of functions for working with arrays that should know everyone.

Example arrays:

$goods = [
    [
        'title' => 'Nokia',
        'price' => '100',
        'qty' => '10',
    ],
    [
        'title' => 'Sony',
        'price' => '120',
        'qty' => '7',
    ],
    [
        'title' => 'LG',
        'price' => '105',
        'qty' => '15',
    ],
];

$car = [
    'brand' => 'Toyota',
    'model' => 'Camry',
    'year' => 2021,
    'speed' => 220,
    'wheels' => 4,
];

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

1. count – counts the number of elements in an array or anything in an object

count($goods, 1); //1 - enables recursive element counting.
//the function returns an int value equal to the number of elements in the array (starting with 0).

2. array_count_values – Counts the number of all values in an array

array_count_values($goods); 
//Returns an array whose keys are the values of array and whose values are the number of the values of array.

3. array_key_exists – checks if the specified key or index is present in the array.

array_key_exists('year', $goods));//check whether the key "years" is in the array "$goods";
//return true or false

4. in_array – checks if a value is present in an array;

in_array(2021, $goods)//checks whether the value "2021" is in the $goods array
//returns true or false

5. array_search – searches an array for a given value and returns the key of the first element found if successful

array_search(3, $goods));
//Returns the key "3" if it was found in the array, false otherwise.

6. array_keys – Returns all or some subset of the keys of an array

array_keys($goods);//Returns an array with all the keys of $car array.

7. array_values – selects all values in an array

array_values($goods);//Returns an indexed array of values.

8. array_unique – removes duplicate values from an array

array_unique($nums);//Removes duplicate values from an array. Returns the filtered array.

9. array_push – adds one or more elements to the end of the array.

array_push($array, 4, 5);

10. array_pop – removes and returns the last element of the array.

array_push($array, 4, 5);

11. array_shift – removes and returns the first element of the array.

$firstElement = array_shift($array); 

12. array_unshift – adds one or more elements to the beginning of the array.

array_unshift($array, 1);

13. array_merge – combines two or more arrays into one

$resultArray = array_merge($array1, $array2);

14. array_slice – returns the selected part of the array, starting at the specified position $start and length $length

$array = [1, 2, 3, 4, 5];
$slicedArray = array_slice($array, 2, 3);
// $slicedArray = [3, 4, 5]

15. array_reverse – returns an array with elements in reverse order.

$array = [1, 2, 3, 4, 5];
$reversedArray = array_reverse($array);
// $reversedArray = [5, 4, 3, 2, 1]
0

More

Leave a Reply

Your email address will not be published. Required fields are marked *

How many?: 22 + 22

lil-code© | 2022 - 2024
Go Top
Authorization
*
*
Registration
*
*
*
*
Password generation