-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheatsheet.php
More file actions
96 lines (75 loc) · 2.09 KB
/
Copy pathcheatsheet.php
File metadata and controls
96 lines (75 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
// Tableaux indexés
$fruits = array("Pomme", "Banane", "Orange");
echo $fruits[2];
// Tableaux associatifs
$ages = [
"Jean" => 25,
"Marie" => 30,
5 => 42,
];
echo $ages['Jean'];
// Tanleaux multidimentionnels
$matrix = [
"Tableau1" => [1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
echo $matrix[1][1];
// Afficher le contenu d'un tableau
echo '<pre>';
var_dump($matrix);
echo '</pre>';
echo '<pre>';
print_r($matrix);
echo '</pre>';
// Fonctions sur les tableaux
// Count retourne le nombre d'éléments d'un tableau
$tab1 = array(1, 2, 3);
echo '<pre>';
count($tab1);
echo '</pre>';
// sort() trie les entiers d'un tableau dans l'ordre croissant
$tab2 = array(2, 3, 1);
sort($tab2);
echo '<pre>';
print_r($tab2);
echo '</pre>';
// rsort() trie les entiers d'un tableau dans l'ordre décroissant
$tab3 = array(2, 3, 1);
rsort($tab3);
print_r($tab3);
echo '<pre>';
print_r($tab3);
echo '</pre>';
// array_reverse() Inverse l'ordre des éléments d'un tableau
$tab4 = array('un', 'deux', 'trois');
$tab4 = array_reverse($tab4);
print_r($tab4);
// array_push() ajoute un élément au tableau
$tab5 = array(1, 2);
array_push($tab5, 3, 4, 5);
echo '<pre>' . print_r($tab5, true) . '</pre>';
// array_pop() supprime le dernier element d'un tableau
$tab6 = array(1, 2, 3);
$last = array_pop($tab6);
echo '<pre>' . $last . '</pre>';
echo '<pre>' . print_r($tab6, true) . '</pre>';
// array_shift() supprime le 1er élément d'un tableau
$tab7 = array(1, 2, 3);
$first = array_shift($tab7);
echo '<pre>' . $first . '</pre>';
echo '<pre>' . print_r($tab7, true) . '</pre>';
// array_unshift() Ajoute un ou plusieurs éléments au début d'un tableau
$tab8 = array(2, 3);
array_unshift($tab8, 1, 22);
echo '<pre>' . print_r($tab8, true) . '</pre>';
// array_merge()
$tab1 = array(1, 2);
$tab2 = array(3, 4);
$result = array_merge($tab1, $tab2);
echo '<pre>' . print_r($result, true) . '</pre>';
// array_slice()
$arr = array(1, 2, 3, 4, 5);
$slice = array_slice($arr, 1, 3);
echo '<pre>' . print_r($slice) . '</pre>';