Chaitanya Lakshmi
  • About Me
  • Job Duties
  • Knowledge Blog
  • Applications
  • Contact Us
    • Linked In
    • Facebook
  • About Me
  • Job Duties
  • Knowledge Blog
  • Applications
  • Contact Us
    • Linked In
    • Facebook

How can I check if a value is already in an array?

4/10/2012

0 Comments

 
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!"; } 
?> 
0 Comments



Leave a Reply.

    Archives

    April 2012
    March 2012
    February 2012

    Categor

    All
    Ajax
    Api
    Cross Site Scripting
    Css
    Curl
    Design Patterns
    Drupal
    Exception Handling
    Htaccess
    Html
    Javascript
    Jquery
    Json
    Linux
    Mysql
    Oops
    Php
    Regular Expressions
    Web 2.0
    Webservices
    Wordpress
    Xhtml

    RSS Feed

Powered by Create your own unique website with customizable templates.