PROGRAMMING

  minte9
MEMORYApp




S R Q
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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

Diff

All the values of $A that do not appear in $B
 
/**
 * Diff returns ...
 * all values in an array not appearing in another
 * 
 * Intersect keeps ...
 * all the values of each element
 */

$A = [1, 2, 3];
$B = [1, 3, 4];

print_r( array_diff($A, $B) ); // 2
print_r( array_intersect($A, $B) ); // 1, 3

Questions    
Last update: 9 days ago
Search