Php
/
Array
- 1 Basics 5
-
Quotes
-
Constants
-
Control structures
-
Reference
-
Integers
- 2 Variables 4
-
Definition
-
Naming
-
Exists
-
Type casting
- 3 Operators 5
-
Aritmetic
-
Bitwise
-
String
-
Comparison
-
Logical
- 4 Function 4
-
Definition
-
Anonymous
-
Reference
-
Variable scope
- 5 Array 9
-
Basics
-
Operations
-
Create
-
KeyValue
-
Search
-
Modify
-
Sort
-
Stacks
-
Storage
- 6 String 9
-
Basics
-
Compare
-
Search
-
Replace
-
Format
-
Regexp
-
Parse
-
Formating
-
Json
- 7 Streams 6
-
File open
-
Read file
-
Read csv
-
File contents
-
Context
-
Ob_start
- 8 Oop 6
-
Object instantiation
-
Class constructor
-
Interfaces, abstract
-
Resource visibility
-
Class constants
-
Namespaces
- 9 Features 9
-
Autoload
-
Class reflection
-
Magic methods
-
Exceptions
-
Late static binding
-
Type hinting
-
SPL
-
PHPUNIT
-
PHAR
- 10 Versions 2
-
Php7.4
-
Php8.0
- 11 Http 4
-
Headers
-
File Uploads
-
Cookies
-
Sessions
- 12 Design Patterns 4
-
Singleton Pattern
-
Observer Pattern
-
Strategy Pattern
-
Registry
- 13 Modern Php 8
-
Composer
-
Slim Framework
-
Autoloader
-
Package
-
Releases
-
Generators
-
Dependency Injection
-
Middleware
- 14 Create Framework 7
-
App
-
Http Foundation
-
Front Controller
-
Routing
-
Render Controller
-
Resolver
-
SoC
- 15 Frameworks 4
-
Symfony v5
-
Laravel v8
-
Laminas v3
-
Codeigniter v4
- 16 Composer 5
-
Guzzle
-
Carbon
-
Faker
-
Math
-
Requests
- 17 Symfony 6
-
Routes
-
Annotations
-
Flex
-
Controllers
-
Doctrine
-
Templating
/
Array modify
➟
➟
Last update: 07-02-2022
Duplicates
array_unique() - Remove duplicate values from an array
$arr = array(1, 2, 'b'=>'green', 2);
$arr = array_unique($arr);
print_r($arr); // [0] => 1 [1] => 2 [b] => green
Replace
array_replace() - Replace elements from passed arrays into the first array
// from PHP 5.3.0
$arr = array('a','b','c');
$arr1 = array(0=>'d');
$arr2 = array(1=>'e');
$arr = array_replace($arr, $arr1, $arr2);
print_r($arr); // [ d, e, c ]
Mapping
Same as array_walk (which needs reference)
function cube($n)
{
return pow($n, 3);
}
$a = array(1,2,3);
$b = array_map('cube', $a);
print_r($b); // [0] => 1 [1] => 8 [2] => 27
Compact example
$b = array_map(
create_function(
'$x', 'return pow($x, 3);'
), array(1,2,3));
print_r($b); // [ 1, 8, 27 ]
Walk
Apply a user function to every member of an array.
$arr = array (1,2,3);
array_walk($arr, create_function('&$x', '$x *= 2;'));
print_r($arr); // [ 2, 4, 6 ]
Padding
Pad array to the specified length with a value.
$array = array(1,2,3);
$arr = array_pad($array, 6, 0);
print_r($arr); // [ 1, 2, 3, 0, 0, 0 ]
echo str_pad(111, 6, 0); // 111000
Slice
Extract a slice of the array
$arr = [1, 2, 3, 4, 5];
print_r(array_slice($arr, 2)); // [ 3, 4, 5 ]
print_r(array_slice($arr, 0, 2)); // [ 1, 2 ]
print_r(array_slice($arr, -2, 1)); // [0] => 4
print_r(array_slice($arr, -2, 1, true));
// [3] => 4 / preserve keys
Splice
Remove a portion of array and replace it with something else.
$arr = array(1, 2, 3, 4, 5);
array_splice($arr, 2);
print_r($arr); // [ 1, 2 ]
$arr = array(1, 2, 3, 4, 5);
array_splice($arr, 2, count($arr), array(10,11));
print_r($arr); // [ 1, 2, 10, 11 ]
Flip
array_flip() - values became keys
$a = array('a', 'b');
$b = array_flip($a);
print_r($a); // [a] => 0 [b] => 1
Reverse
Inverts the order of the array's elements.
$a = array('a', 'b');
$b = array_reverse($a);
print_r($b); // [0] => b [1] => a
➥ Questions