-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssociativeCompanyExample.php
More file actions
53 lines (41 loc) · 1.35 KB
/
Copy pathAssociativeCompanyExample.php
File metadata and controls
53 lines (41 loc) · 1.35 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
<?php
$company = [
"departments" => [
"HR" => [
"manager" => "John Doe",
"staff" => ['Alice', 'Bob', 'Carol']
],
"IT" => [
"manager" => "Jane Doe",
"staff" => ['Dave', 'Eve', 'Frank']
],
],
"location" => "India",
"founded" => 2000,
];
/**
* Using foreach loop to print the array:-
*
* In this for each loop, the "implode" function basically expands the array with the separator we have
* given to it as the first argument.
*/
foreach ($company["departments"] as $key => $value) {
echo "Department: $key<br>";
echo "Manager: " . $value["manager"] . "<br>";
echo "Employees: " . implode(", ", $value["staff"]) . "<br><br>";
};
echo "Company Location: " . $company["location"] . "<br>";
echo "Company Founded: " . $company["founded"] . "<br>";
/**
* Now we have to add some data in this array:-
*/
$company["departments"]["HR"]["salary"] = [5000, 6000, 7000];
$company["departments"]["IT"]["salary"] = [8000, 9000, 10000];
echo "<br><br><b>New Array: -</b> <br><br>";
foreach ($company["departments"] as $key => $value) {
echo "Department: $key<br>";
echo "Manager: " . $value["manager"] . "<br>";
echo "Employees: " . implode(", ", $value["staff"]) . "<br><br>";
echo "Salary: " . implode(", ", $value["salary"]) . "<br><br>";
};
?>