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-operations
➟
➟
Last update: 29-10-2021
Array Operations
Exists
To check if array exists use is_array, not count.
$a = array();
echo count($a); // 0
echo is_array($a); // true
Reverse
Inverse the order of the arrray's elements with array_reverse.
$a = array('a', 'b');
$b = array_reverse($a);
print_r($b); // [0] => b [1] => a
Next
Reset, key, next, current are used when working with array internal pointer.
$arr = array('a' => 1, 'b' => 2, 'c' => 3);
reset($arr);
while (key($arr) !== NULL) {
echo key($arr) . current($arr) . " "; // a1 b2 c3
next($arr);
}
Array map
Array_map is the 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); // [1, 8, 27]
$result = array_map
(
create_function('$x', 'return pow($x, 3);'), array(1,2,3)
);
print_r($result); // [ 1, 8, 27 ]
Array walk
Apply a user function to every member of an array.
$arr = array(1,2,3,4);
function setDouble($value)
{
echo $value * 2; // 2 4 6 8
}
array_walk($arr, 'setDouble');
$arr = array(1,2,3,4);
function setDouble(&$value) // reference
{
$value *= 2;
}
array_walk($arr, 'setDouble');
print_r($arr); // [2, 4, 6, 8]
Keys destruction
Sorting destroys all the keys and rearange elements.
$arr = array('a'=>'foo', 'b'=>'bar', 'c'=>'baz');
sort($arr);
print_r($arr); // [0] => bar [1] => baz [2] => foo
Add element
$arr = array();
array_push($arr, 'bar', 'baz'); // add at the end
print_r($arr); // [bar, baz]
When only one value is being pushed [] is faster (no function call).
$arr = array('bar', 'baz');
$arr[] = 'foo';
var_dump($arr); //[bar, baz, foo]
Extract last
$arr = array('bar', 'baz');
$last = array_pop($arr);
echo $last; // baz
var_dump($arr); // array(1) { [0]=> string(3) "bar" }
Extract first
$arr = array('bar', 'baz');
$first = array_shift($arr);
print_r($arr); // [0] => baz
Add first
$arr = array('bar', 'baz');
array_unshift($arr, 'foo');
print_r($arr); // [foo, bar, baz]
➥ Questions