To create a set of numbers from 1 to N in PHP, you can use a loop to add each number to an array and then convert the array to a set using the array_unique function.
In this example,
numberArray
.[]
notation.array_unique
function to remove duplicate elements and convert the array to a set.<?php
$numberArray = [];
for ($i = 1; $i <= 5; $i++) {
$numberArray[] = $i;
}
$numberSet = array_unique($numberArray);
print_r($numberSet);
?>
[1, 2, 3, 4, 5]
In this example,
numberArray
.[]
notation.array_unique
function to remove duplicate elements and convert the array to a set.<?php
$numberArray = [];
for ($i = 1; $i <= 10; $i++) {
$numberArray[] = $i;
}
$numberSet = array_unique($numberArray);
print_r($numberSet);
?>
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
In this tutorial, we learned How to create a Set of Numbers from 1 to N in PHP language with well detailed examples.