-
Notifications
You must be signed in to change notification settings - Fork 1
/
07_arrays_functions.php
69 lines (51 loc) · 1.39 KB
/
07_arrays_functions.php
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
<?php
/*
* The array functions allow you to access and manipulate arrays. Simple and multi-dimensional arrays are supported.
* more on the link: https://www.w3schools.com/php/php_ref_array.asp
*As funções de array permitem acessar e manipular arrays. Matrizes simples e multidimensionais são suportadas.
*mais no link: https://www.w3schools.com/php/php_ref_array.asp
*/
$fruits = ['apple', 'orange', 'pear'];
//Eng: Get length || Pt: obtendo a tamanho
echo "<hr>fruit array length: ". count($fruits);
//search array
var_dump('<hr> search array:' . in_array('apple', $fruits) .'<hr>');
//add to array
$fruits[] = 'grape';
array_push($fruits, 'mango');
//remove from array
array_pop($fruits);
array_shift($fruits);
//split into 2 chunks
$chunked_array = array_chunk($fruits, 2);
//print
print_r($fruits);
//number
echo "<hr>";
$arr1 = [1,2,3];
$arr2 = [4,5,6];
$arr3 = array_merge($arr1, $arr2);
print_r($arr3);
echo "<hr>";
$a= ['green', 'red', 'yellow'];
$b= ["avacado", "apple", "banana"];
$c = array_combine($a, $b);
print_r($c);
echo "<hr>";
$keys = array_keys($c);
print_r($keys);
echo "<hr>";
$flipped = array_flip($c);
print_r($flipped);
echo "<hr>";
$numbers = range(1,20);
print_r($numbers);
echo "<hr>";
$newNumbers = array_map(function($number) {
return "Number: ${number}";
}, $numbers);
print_r($newNumbers);
/**
* more on the link: https://www.w3schools.com/php/php_ref_array.asp
*/
?>