MarcoFaul
9/3/2017 - 1:36 PM

Fancy Array Stuff

Fancy Array Stuff #php

<?php

######################################## Fancy Array stuff ########################################
### defaultArray ###
$defaultArray = [
    'clothes' => 't-shirt',
    'size' => 'medium',
    'color' => 'blue',
];
### defaultArray ###

# Import variables into the current symbol table from an array
extract($defaultArray, EXTR_IF_EXISTS); // $clothes, $size, $color


array_keys($defaultArray); // ['clothes', 'size', 'color']
array_values($defaultArray); // ['t-shirt', 'medium', 'blue']
array_flip($defaultArray);  // turn key-values


$indexArray = array('yolo', 'hodor', 'dunno');

# split array into given variables
list($firstValue, $secondValue, $thirdValue) = $indexArray; // yolo, hodor, dunno


$string = 'hello|wild|world';
# first split array and use list to assign array to variables
list($hello, , $world) = explode('|', $string);


$cities = ['Berlin', 'KYIV', 'Amsterdam', 'Riga'];
# applies strtolower to all array values
$aliases = array_map('strtolower', $cities); // ['berlin', 'kyiv, 'amsterdam', 'riga']


$keys = ['sky', 'grass', 'orange'];
$values = ['blue', 'green', 'orange'];

$array = array_combine($keys, $values); // sky => blue, grass => green, orange => orange

$multiArray = [
    [1, 2],
    [3, 4],
    [5, 6],
];
foreach ($multiArray as list($a, $b)) {
    $c = $a + $b;
    echo($c . ', '); // 3, 7, 11,
}

# use own function on given array. Third param is for surpassing everything or just key|value
array_filter($numbers, 'FunctionNameThatShouldBeCalled', ARRAY_FILTER_USE_BOTH);

$array = [1, 1, 1, 1, 2, 2, 2, 3, 4, 5, 5];
$uniques = array_unique($array); // 1,2,3,4,5


$array = [
    ['id' => 1, 'title' => 'tree'],
    ['id' => 2, 'title' => 'sun'],
    ['id' => 3, 'title' => 'cloud'],
];

# Return the values from a single column in the input array
# !!! php 7 forever. You can use objects aswell
$ids = array_column($array, 'id'); // 1,2,3


$array1 = [1, 2, 3, 4];
$array2 =       [3, 4, 5, 6];

$diff = array_diff($array1, $array2); // [0 => 1, 1 => 2]
$intersect = array_intersect($array1, $array2); // [2 => 3, 3 => 4]



$numbers = [1, 2, 3, 4, 5];
# Calculate the sum of values in an array
array_sum($numbers); // 15

# Calculate the product of values in an array
array_product($numbers); // 120

$things = ['apple', 'apple', 'banana', 'tree', 'tree', 'tree'];
$values = array_count_values($things); // [apple] => 2, [banana] => 1, [tree] => 3


$array1 = ['a' => 'a', 'b' => 'b', 'c' => 'c'];
$array2 = ['a' => 'A', 'b' => 'B', 'D' => 'D'];

$merge = array_merge($array1, $array2); // array("a"=> "A", "b"=> "B", "c"=> "c", "D"=> "D");


$bind = array_fill(0, 5, '?'); // ['?', '?', '?', '?', '?']


$letters = range('a', 'z'); // ['a', 'b', ..., 'z']
$hours = range(0, 23); // [0, 1, 2, ..., 23]

$numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
$top = array_slice($numbers, 0, 3); // [1, 2, 3]


$values = ['say  ', '  bye', ' ', ' to', ' spaces ', '   '];
$words = array_filter(array_map('trim', $values)); // ['say', 'bye', 'to', 'spaces']