PHP Questions Home
Categories
Arrays
Files
Forms
Creating PDFs
PHP Errors
Functions
Images
MySQL
Numbers
Others
Strings
Website

PHP Forums
Forums Index
MySQL Forum
PHP Forum
Flash/Actionscript
Javascript
Web design

More PHP
PHP Functions
Top Questions
Ask a Question
PHP Interview Questions
PHP Jobs
Ecommerce
IT Questions
Business Websites

Sponsors

sponsor ad
My Salary
PHP Developer Salary
Advertise Here

How do I find if a value is already in an array or not?


This can easily be done with the in_array() function.

It is strange how we tend not to need to use this function the first few times we use PHP, but then when occasions crop up that we need to use this function we find that there are many, many times where we need to know if a value is already in an array or not.

Here is how to use this simple function:

<?php

$values 
= array(1,20,10);

$masterarray range(1,10);//contains 1 - 10

foreach($values as $test) {

if(
in_array($test,$masterarray)) { echo "$test is in the masterarray\n";}

}
//prints:
//1 is in the masterarray
//10 is in the masterarray
?>



ASK A QUESTION

More arrays PHP Questions

I have an array of numbers and many repeat, how to list all items in order of maximum occurence?

HOW CAN I DELETE ALL THE ELEMENT OF THE ARRAY

Hi. i have a music samples website. in one table, id like a different sample to appear every time the page is refreshed. how do i acheve this? how can i alterate and retrieve these bloody mp3's? surely i can use a PHP script? thankyou

How do I sort the members of an array by value?

How do I sort alphanumeric array data correctly?



Custom Search