PHP Array

Remove specific element by value from an array in PHP?

Description:

You need to write a program in PHP to remove specific element by value from an array using PHP program.

Instructions:

  • Take an array with list of month names.
  • Take a variable with the name of value to be deleted.
  • You can use PHP array functions or foreach loop.

Solution 1: Using array_search()

With the help of array_search() function, we can remove specific elements from an array.

<?php
$delete_item = 'march';
// take a list of months in an array
$months = array('jan', 'feb', 'march', 'april', 'may');
if (($key = array_search($delete_item, $months)) !== false) {
    unset($months[$key]);
}
 
// print array to see latest values
var_dump($months);
?>
Tutorials Class - Output Window

array(4) { [0]=> string(3) “jan” [1]=> string(3) “feb” [3]=> string(5) “april” [4]=> string(3) “may” }


Solution 2: Using foreach()

By using foreach() loop, we can also remove specific elements from an array.

<?php
$delete_item = 'april';
// take a list of months in an array
$months = array('jan', 'feb', 'march', 'april', 'may'); // for april, the key is 4
foreach (array_keys($months, $delete_item) as $key) {
    unset($months[$key]);
}

// print array to see latest values
var_dump($months);
?>
Tutorials Class - Output Window

array(4) { [0]=> string(3) “jan” [1]=> string(3) “feb” [3]=> string(5) “april” [4]=> string(3) “may” }


Solution 3: Using array_diff()

With the help of array_diff() function, we also can remove specific elements from an array.

<?php
$delete_item = 'april';
// take a list of months in an array
$months= array('jan', 'feb', 'march', 'april', 'may');
$final_months= array_diff($months, array($delete_item));
 
// print array to see latest values
var_dump($final_months);
?>
Tutorials Class - Output Window

array(4) { [0]=> string(3) “jan” [1]=> string(3) “feb” [2]=> string(5) “march” [4]=> string(3) “may” }

PHP Array to String Conversion (favourite colours chosen by user)

Description:
Write an exercise for PHP Array to String Conversion.

  • Create a form that accept name as well as colors
  • After submission, form data will be sent to another page
  • Display select colors (as a list) and user name
  • User name & colors selection is mandatory

View Solution/Program

<html>
<head>
<title>
array to string
</title>
</head>
<body>
	<form action="data.php" method="post">
		Username: <input type="text" name="username" placeholder="enter name" required/><br/><br/> 
		Select your favourite colors:<br/>
		Red<input type="checkbox" name="check_list[]" value="red"/><br/>
		Blue<input type="checkbox" name="check_list[]" value="blue"/><br/>
		Green<input type="checkbox" name="check_list[]" value="green"/><br/>
		Yellow<input type="checkbox" name="check_list[]" value="yellow"/><br/>
		Pink<input type="checkbox" name="check_list[]" value="pink"/><br/>
		Black<input type="checkbox" name="check_list[]" value="black"/><br/>
		White<input type="checkbox" name="check_list[]" value="white"/><br/><br/>
		<input type="submit" name="submit" value="Submit"/><br/>
	</form>
</body>
</html>
<?php
if (isset($_POST['submit'])) {
    if (!empty($_POST['check_list'])) {
        // Counting number of checked checkboxes.
        $checked_count = count($_POST['check_list']);
        $name = $_POST['username'];
        echo $name . " 's favourite colors are " . $checked_count . " option(s): <br/>";
        // Loop to store and display values of individual checked checkbox.
        foreach ($_POST['check_list'] as $selected) {
            echo "<p>" . $selected . "</p>";
        }
    } else {
        echo "<b>Please Select Atleast One Option.</b>";
    }
}

How to check if an array is a subset of another in PHP?

Description:

You need to find whether an array is subset of another array.

  • Let us suppose that there are two arrays.
  • First array is large which have 6 values.
  • Second array is small which have 2 values
  • Find if second array is subset of first which means that all values of second array should exists in first array.
  • You can use Decision Making Statements.

PHP Program to find if an array is a subset of another:

<?php
// Define two array
$array1 = array('a','1','2','3','4');
$array2 = array('a','3');

// intersect/matched values should be equal to first array
if (array_intersect($array2, $array1) === $array2) {
  echo "It is a subset";
} else {
   echo "Not a subset";
}
?>
Tutorials Class - Output Window

It is a subset