Created
March 15, 2021 06:43
-
-
Save cba85/3db251b44f43c6bd6844853fc78f9e32 to your computer and use it in GitHub Desktop.
PHP array (For demonstration purpose only)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// For demonstration purpose only | |
// Arrays | |
$names = ['clement', 'tomy', 'jacques']; | |
print_r($names); | |
echo $names[0]; | |
$people = [ | |
'Clement' => [ | |
'name' => 'Barbaza', | |
'first_name' => "Clément", | |
'email' => 'clement@webstart.com', | |
'classes' => ['dev 2', 'dev 3'], | |
], | |
'Tomy' => [ | |
'name' => 'Spagnoletti', | |
'first_name' => 'Tomy', | |
'email' => 'tomy@webstart.com', | |
'classes' => ['dev 1', 'dev 2'], | |
] | |
]; | |
print_r($people); | |
echo $people['clement']['email']; | |
// Counting | |
echo count($names); | |
echo sizeof($names); | |
$count = array_count_values($names); | |
print_r($count); | |
echo count($people); | |
// Loops | |
foreach($people as $key => $person) { | |
echo $key . ' ' . $person['name'] . ': ' . $person['email'] . "\n"; | |
foreach ($person['classes'] as $class) { | |
echo $class . "\n"; | |
break 2; | |
} | |
echo "\n"; | |
} | |
// In array | |
$in = in_array('tomy', $names); | |
print_r($in); | |
// Explode | |
$allowed = ['jpg', 'jpeg', 'png']; | |
$filename = 'image.bmp'; | |
$parts = explode('.', $filename); | |
$extension = end($parts); // $parts[1]; | |
if (in_array($extension, $allowed)) { | |
echo 'Upload authorized'; | |
} | |
// Implode | |
$string = implode(', ', $allowed); | |
echo $string; | |
// Keys | |
$array = ['clement' => 31, 'tomy' => 81, 'jacques' => 75]; | |
$keys = array_keys($array); | |
print_r($keys); | |
// Mapping | |
foreach($people as $person) { | |
echo $person['first_name'] . ' ' . $person['name'] . "\n"; | |
} | |
$people = array_map(function($person) { | |
$person['full_name'] = $person['first_name'] . ' ' . $person['name']; | |
return $person; | |
}, $people); | |
print_r($people); | |
// Sum | |
$numbers = [1, 2, 3, 4]; | |
echo array_sum($numbers); | |
// Filters | |
$users = [ | |
['username' => 'clement', 'score' => 0], | |
['username' => 'tomy', 'score' => 0], | |
['username' => 'jacques', 'score' => 50], | |
]; | |
$users = array_filter($users, function($user) { | |
if ($user['score'] > 0) { | |
return true; | |
} | |
}); | |
print_r($users); | |
// Walk | |
array_walk($users, function(&$user, $index) { | |
$user['username'] = 'Michel'; | |
return $user; | |
}); | |
print_r($users); | |
function changeName(&$user, $index) { | |
$user['username'] = 'Michel'; | |
return $user; | |
} | |
array_walk($users, 'changeName'); | |
print_r($users); | |
// Sorting | |
$names = ['Clement', 'Tomy', 'Jacques']; | |
$numbers = [1, 8, 4, 12, 13, 53]; | |
sort($names); | |
rsort($names); | |
print_r($names); | |
$names = ['Clement' => 33, 'Tomy' => 32, 'Jacques' => 35]; | |
asort($names); | |
arsort($names); | |
print_r($names); | |
array_multisort($numbers, SORT_DESC); | |
print_r($numbers); | |
$files = ['img12.png', 'img10.png', 'img2.png', 'img1.png']; | |
array_multisort($files, SORT_NATURAL); | |
print_r($files); | |
// Push and pop | |
$names = ['Clement', 'Tomy', 'Jacques']; | |
$names[] = 'Michel'; | |
$names[] = 'Julie'; | |
array_push($names, 'Michel', 'Julie'); | |
print_r($names); | |
$popped = array_pop($names); | |
print_r($popped); | |
// Reduce | |
$items = [ | |
['price' => 5], | |
['price' => 2], | |
['price' => 10], | |
]; | |
$total = array_reduce($items, function ($cart, $item) { | |
if (!$cart) { | |
return $item; | |
} | |
$cart['price'] += $item['price']; | |
return $cart; | |
}); | |
print_r($total['price']); | |
// Random | |
$random = rand(0, 2); | |
$randomName = $names[$random]; | |
echo $randomName; | |
$randomKey = array_rand($names, 1); | |
echo $randomKey; | |
echo $names[$randomKey]; | |
// Merge arrays | |
$otherNames = ['Julien', 'Romain', 'Vincent']; | |
$allNames = array_merge($names, $otherNames); | |
print_r($allNames); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment