- BASICS
- Quotes
- Constants
- Control Structures
- Reference
- Number Systems
- VARIABLES
- Definition
- Variable Variable
- Exists
- Type Casting
- OPERATORS
- Aritmetic
- Bitwise
- String
- Comparison
- Logical
- FUNCTION
- Definition
- Anonymous
- Reference
- Variable Arguments
- ARRAY
- Basics
- Operations
- Create
- Search
-
Modify
- Sort
- Storage
- STRING
- String Basics
- String Compare
- String Search
- String Replace
- String Format
- String Regexp
- String Parse
- Formating
- Json
- STREAMS
- File Open
- Read File
- Read Csv
- File Contents
- Context
- Ob_start
- OOP
- Object Instantiation
- Class Constructor
- Interfaces, Abstract
- Resource Visibility
- Class Constants
- Namespaces
- HTTP
- Headers
- File Uploads
- Cookies
- Sessions
Add
Add or remove element from array
/**
* array_push() add at the end
* array_shift() extract first
* array_unshift() add at beggining
* arrau_pop() extract last
*/
$A = [1, 2];
$B = [2, 3];
$C = [1, 2];
$D = [2, 4];
array_push($A, 3);
$A[] = 4; // faster
print_r($A); // 1,2,3,4
echo array_shift($B); // 2 (first)
array_unshift($C, 100);
print_r($C); // 100,1,2
echo array_pop($D); // 4
Slice
Extract a slice of the array
/**
* array_slice(), array_splice()
*
* Extract or remove a portion from array
*/
$A = [1, 2, 3, 4, 5];
print_r(array_slice($A, 2)); // 3, 4, 5
print_r(array_slice($A, 0, 2)); // 1, 2
print_r(array_slice($A, -2, 1)); // [0] => 4
print_r(array_slice($A, -2, 1, true)); // [3] => 4 (preserve keys)
$B = array(1, 2, 3, 4, 5);
print_r(array_splice($B, 2)); // [ 1, 2 ]
Questions and answers
Remove duplicates values from an array?
- a) array_diff($array);
- c) array_unique($array);
Reverse the order of array elements
- a) $b = array_reverse($a);
- b) $b = array_flip($a);
- a) [3, 4, 5]
- b) [1, 2]