Open Source Tutorials - Open Source Training
Open source training & tutorials from experienced, passionate people
chrome icon firefox icon ie icon opera icon safari icon Sings in these Browsers
A- A A+

By A Web Design

Welcome, Guest
Username Password: Remember me

value is already in an array
(1 viewing) (1) Guest
All your queries regarding
PHP Installation,
Binding Apache-PHP-MySQL,
and tonnes more... will be solved in this forum
  • Page:
  • 1
TOPIC:

value is already in an array

value is already in an array 1 year, 9 months ago #103

  • manav
  • OFFLINE
  • Senior Boarder
  • Posts: 51
  • Karma: 0
Hi,

I am new to PHP and want to know how can I check if a value is already in an array?

Re:value is already in an array 1 year, 9 months ago #104

  • shama
  • OFFLINE
  • Senior Boarder
  • Posts: 63
  • Karma: 0
If you wish to check whether a value is already stored in an array or not, then use the in_array function.

This is useful when you don't want any duplicates in the array and therefore only want to add a value if it's not already there. The first argument is the string you are testing for and the second is the array you are checking against.

Here is an example of in_array in action:
<?php
$values = array("banana","apple","pear","banana");
$newvalue = "pear";
if (in_array($newvalue,$values))
{
echo "$newvalue is already in the array!";
}
?>

The most common mistake made with in_array is to pass the arguments in the wrong order to the function, so take care!
  • Page:
  • 1
Time to create page: 2.71 seconds
OSV Newsletter


Receive HTML?

NOTE: To prevent subscription to the OSV newsletter, uncheck the checkbox above.
Guest Blog for OSV
Free Ebook Download