PHP 3 (Arrays-Associative-Strings)
PHP 3 (Arrays-Associative-Strings)
Associati ve Arrays,
Strings and String Operati ons
SoftUni Team
Technical Trainers
Software University
http://softuni.bg
Table of Contents
1. Associative Arrays
Array Manipulation
Multidimensional Arrays
2. Strings
String Manipulation
Regular Expressions
2
Associative Arrays
Associative Arrays (Maps, Dictionaries)
Associative arrays are arrays indexed by keys
Not by the numbers 0, 1, 2, 3, …
$phonebook = [];
$phonebook["John Smith"] = "+1-555-8976"; // Add
$phonebook["Lisa Smith"] = "+1-555-1234";
$phonebook["Sam Doe"] = "+1-555-5030";
$phonebook["Nakov"] = "+359-899-555-592";
$phonebook["Nakov"] = "+359-2-981-9819"; //
6
Iterating Through Associative Arrays
foreach ($array as $key => $value)
Iterates through each of the key-value pairs in the array
7
Problem: Sum by Town
Read towns and incomes (like shown below) and print a array
holding the total income for each town (see below)
Sofia Print the towns in their natural order as
20 object properties
Varna
3
Sofia
["Sofia" => "25","Varna" => "7"]
5
Varna
4
8
Solution: Sum of Towns
$arr = ['Sofia','20', 'Varna','10', 'Sofia','5'];
$sums = [];
for ($i = 0; $i < count($arr); $i += 2) {
list($town, $income) = [$arr[$i], $arr[$i+1]];
if ( ! isset($sums[$town]))
$sums[$town] = $income;
else
$sums[$town] += $income; list($town,..)
} Assign variables as if
they were an array
print_r($sums);
9
Problem: Counting Letters in Text
$text = "Learning PHP is fun! ";
$letters = [];
$text = strtoupper($text);
for ($i = 0; $i < strlen($text); $i++) {
$char = $text[$i];
if (ord($char) >= ord('A') && ord($char) <= ord('Z')) {
if (isset($letters[$char])) {
$letters[$char]++;
} else { isset($array[$i])
$letters[$char] = 1; checks if the key exists
}
}
}
print_r($letters);
10
Strings
Strings
A string is a sequence of characters
Can be assigned a literal constant or a variable
Text can be enclosed in single (' ') or double quotes (" ")
<?php
$person = '<span class="person">Mr. Svetlin Nakov</span>';
$company = "<span class='company'>Software University</span>";
echo $person . ' works @ ' . $company;
?>
12
String Syntax
Single quotes are acceptable in double quoted strings
echo "<p>I'm a Software Developer</p>";
14
Heredoc Syntax
Heredoc syntax <<<"EOD" .. EOD;
$name = "Didko";
$str = <<<"EOD"
My name is $name and I am
very, very happy.
EOD;
echo $str;
/*
My name is Didko and I am
very, very happy.
*/
15
Nowdoc Syntax
Nowdoc syntax <<<'EOD' .. EOD;
$name = "Didko";
$str = <<<'EOD'
My name is $name and I am
very, very happy.
EOD;
echo $str;
/*
My name is $name and I am
very, very happy.
*/
16
String Concatenation
In PHP, there are two operators for combining strings:
Concatenation operator .
Concatenation assignment operator .=
<?php
$homeTown = "Madan";
$currentTown = "Sofia";
$homeTownDescription = "My home town is " . $homeTown . "\n";
$homeTownDescription .= "But now I am in " . $currentTown;
echo $homeTownDescription;
18
Manipulating Strings
Accessing Characters and Substrings
strpos($input, $find) – a case-sensitive search
Returns the index of the first occurrence of string in another string
26
Problem: Concatenate and Reverse Strings
Read an array of strings, concatenate them and reverse them
27