- 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
Chunks
Split an array into separate parts.
/**
* Create an array containing a range of elements ...
* then split into chunks
*/
$data = range(1,100);
$chunks = array_chunk($data, 2);
foreach($chunks as $v) {
echo " $v[0], $v[1] \n";
}
# 1, 2 / 3, 4 ... 99, 100
Merge
Merge one or more arrays.
/**
* Merge one or more arrays
*
* If arrays have the same string keys ...
* the later value for that key will be overwriten
*
* If don't want to ovewrite use the + array union operator
*/
$A = array_merge((array) 'foo', [1, 2]);
$B = array_merge(['color'=> 'red', 1], ['a', 'color'=>'green']);
$C = ['a' => 'A', 1] + ['a' => 'B', 2, 3];
echo 'A: ' . print_r($A, true); // foo, 1, 2
echo 'B: ' . print_r($B, true); // green, 1, a
echo 'C: ' . print_r($C, true); // A, 1, 3
Questions and answers
Create a new array containing 10 numbers
- a) range(0, 10);
- b) range (1, 100, 10);
Append an array to another and not overwrite the keys?
- a) $arr1 + $arr2;
- b) array_merge($arr1, $arr2);
How do you split an array into parts?
- a) extract($array, $step);
- b) array_chunk($array, $step);