PHP Array
Remove specific element by value from an array in PHP?
Description: Write a PHP program to remove a specific element, by its value, from an array.
Rules and hints:
- Take an array with the names of some months.
- Take a variable with the value that you want to remove.
- You can use array functions or a foreach loop.
Solution 1: array_search() and unset()
array_search() finds the key of the value, and unset() removes that key. The other keys are not changed.
<?php
$months = ["January", "February", "March", "April", "May"];
$remove = "March";
$key = array_search($remove, $months);
if ($key !== false) {
unset($months[$key]);
}
print_r($months);
?>
Output:
Array
(
[0] => January
[1] => February
[3] => April
[4] => May
)
Solution 2: array_diff()
array_diff() removes all matching values (also if the value is many times in the array). Use array_values() to get new keys 0, 1, 2…
<?php
$months = ["January", "February", "March", "April", "May"];
$remove = "March";
$months = array_values(array_diff($months, [$remove]));
echo implode(", ", $months);
?>
Output:
January, February, April, May
Solution 3: array_filter()
<?php
$months = ["January", "February", "March", "April", "May"];
$remove = "March";
$months = array_filter($months, fn($month) => $month !== $remove);
echo implode(", ", $months);
?>
Output:
January, February, April, May
Note that solution 1 removes only the first match, but solutions 2 and 3 remove every match.
PHP Array to String Conversion (favourite colours chosen by user)
Description: Write a PHP exercise for array to string conversion.
Rules and hints:
- Create a form that accepts a name and some colors (checkboxes).
- After the submit, the data goes to another page.
- Show the user name and the selected colors as a list.
- The name and at least one color are required.
Step 1: the form (index.html)
<form action="data.php" method="post">
Name: <input type="text" name="username" required><br><br>
Select your favourite colors:<br>
<input type="checkbox" name="colors[]" value="Red"> Red
<input type="checkbox" name="colors[]" value="Green"> Green
<input type="checkbox" name="colors[]" value="Blue"> Blue
<input type="checkbox" name="colors[]" value="Yellow"> Yellow<br><br>
<button type="submit">Send</button>
</form>
The name colors[] (with the square brackets) tells PHP to collect all the selected values in an array.
Step 2: data.php
<?php
$username = trim($_POST["username"] ?? "");
$colors = $_POST["colors"] ?? [];
if ($username === "" || empty($colors)) {
exit("Name and at least one color are required.");
}
echo "Hello " . htmlspecialchars($username) . "<br>";
echo "Your favourite colors: " . htmlspecialchars(implode(", ", $colors));
?>
Output:
Hello Robin
Your favourite colors: Red, Blue
How it works
$_POST["colors"]is an array of the checked boxes.implode(", ", $colors)converts the array to a text. See how to convert an array to a string.htmlspecialchars()protects the page from XSS.
How to check if an array is a subset of another in PHP?
Description: Write a PHP program to find out if an array is a subset of another array.
Rules and hints:
- The first array is large, and the second array is small.
- The second array is a subset if all of its values exist in the first array.
- You can use decision making statements.
PHP program to check if an array is a subset of another
<?php
$array1 = ["a", "1", "2", "3", "4"];
$array2 = ["a", "3"];
$array3 = ["a", "z"];
// array_diff gives the values of the first array that are missing in the second
if (count(array_diff($array2, $array1)) === 0) {
echo "array2 is a subset of array1<br>";
} else {
echo "array2 is not a subset of array1<br>";
}
if (count(array_diff($array3, $array1)) === 0) {
echo "array3 is a subset of array1";
} else {
echo "array3 is not a subset of array1";
}
?>
Output:
array2 is a subset of array1
array3 is not a subset of array1
How it works
array_diff($array2, $array1)returns the values of $array2 that are not in $array1.- If nothing is missing (the count is 0), all values exist, so it is a subset.
- “z” is not in $array1, so $array3 is not a subset.
Another way: the result of array_intersect($array2, $array1) must have the same count as $array2. See PHP Array Functions.