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.

Output:  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.

Output:  array(4) { [0]=> string(3) “jan” [1]=> string(3) “feb” [2]=> string(5) “march” [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.

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


PHP Array to String Conversion (favorite colors 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

Click below to View Solution/Program


Amit’s favorite colors are 3 option(s):

red

green

blue