Skip to content

Chapter 5 of 7

Working with Arrays and Strings

PHP's array is a versatile ordered map that doubles as a list, and the language provides many built-in functions for transforming and querying them. array_map() applies a callback to each element and returns a new array, making it the canonical way to transform collections, often paired with arrow functions such as fn($n) => $n * 2. For combining arrays, you can use array_merge(), which re-indexes numeric keys, or the spread operator [...$a, ...$b] introduced in PHP 7.4, which performs the same job inline. With string keys, both approaches let later values overwrite earlier ones.

Filtering and reduction are equally important. array_filter() returns elements for which the supplied callback returns true, and when called without a callback it removes any value considered falsy. array_reduce() collapses an array to a single value by feeding each element through a callback that updates a running carry, perfect for summing numbers, building a string, or accumulating complex state. When you need to check membership, in_array() searches for a value while array_key_exists() searches for a key, and you can pass true as in_array's third argument to compare values strictly with the same rules as the === operator.

String manipulation is just as central. explode() splits a string into an array around a delimiter, and implode() does the inverse, joining array elements into a string with a chosen glue. PHP 8 introduced three highly readable helpers: str_contains() checks for a substring, str_starts_with() checks a prefix, and str_ends_with() checks a suffix, all returning booleans. For formatted output, sprintf() returns a string built from placeholders such as %s for strings, %d for integers, and %f for floats, allowing precise control over the final text without resorting to concatenation.

All chapters
  1. 1Object-Oriented Foundations
  2. 2Inheritance, Interfaces, and Traits
  3. 3Namespaces and Dependency Management
  4. 4The PHP Type System
  5. 5Working with Arrays and Strings
  6. 6Databases, Sessions, and Error Handling
  7. 7Closures, Generators, and Magic Methods

Drill it

Reading is not remembering. These come from the Php Programming deck:

Q

What is a class in PHP OOP?

A class is a blueprint for creating objects. It defines properties (variables) and methods (functions) that the objects will have.class User { public string $na...

Q

What is the difference between public, protected, and private visibility in PHP?

public – accessible from anywhere.protected – accessible within the class and its subclasses.private – accessible only within the defining class.These are known...

Q

How do you create an object from a class in PHP?

Use the new keyword:$user = new User();This calls the class constructor (__construct) if one is defined.

Q

What is the purpose of the __construct() magic method?

__construct() is the constructor that runs automatically when an object is created with new.public function __construct(public string $name) {}It initializes ob...