- 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
Map, Walk
Apply a user function to every member of an array.
/**
* array_map, array_walk()
*
* Array walk needs reference
*/
function cube($n)
{
return pow($n, 3);
}
$A = [1, 2, 3];
$B = [1, 2, 3];
$C = [1, 2, 3];
$D = [1, 2, 3];
$A = array_map('cube', $A);
$B = array_map(fn($x) => pow($x, 3), $B);
array_walk($C, fn(&$x) => $x = pow($x, 3));
array_walk($D, fn(&$x) => $x *= 2); // Look Here
print_r($A); // 1 8 27
print_r($B); // 1 8 27
print_r($C); // 1 8 27
print_r($D); // 2 4 6
Next
We can use internal pointer when working with arrays.
/**
* Reset, key, next, current ...
*/
$arr = ['a'=>1, 'b'=>2, 'c'=>3];
reset($arr);
while (key($arr) !== NULL) {
print_r( key($arr) . current($arr) . " " ); // a1 b2 c3
next($arr);
}
Exists
Check to see if array exists or not.
/**
* Check if array exists
* Use is_array() / not count()
*/
$a = array();
echo count($a); // 0
echo is_array($a); // true
Last update: 574 days ago