answersLogoWhite

0


Best Answer

To check if a value is already in an array you will need to use the in_array function like shown below!

<?php

$values = array("Pizza","hello","cheese","fred");

$newvalue = "hello";

if (in_array($newvalue, $values))

{

echo "$newvalue is already in the array!";

}

?>

User Avatar

Wiki User

13y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How can you check if a value is already in an array in PHP?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How do you count the vowels in a string using PHP?

build an array of vowels then do a foreach on the array and then explode the string on the array value and the answer is -1 of the result


How do you empty an array in PHP?

To empty an array in PHP you need to use the unset function like shown below: &lt;?php $array = array('hello', 'hi', 'weee'); unset($array); // empty ?&gt;


How do you pick a random value from a PHP array?

To pick random results from an array in PHP you will need to use the array_rand() function, you can select the amount of results you wish it too select too. &lt;?php $array = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); $rand_results = array_rand($array, 3); // will select 3 random results from the array and store in a new array ?&gt;


How do you print an array in php?

There are a few methods however the following is the method that will allow you to do the most with the information afterwards. foreach($array as $key =&gt; $value){ echo '[' . $key . '] ' . $value; #$key becomes the array key and value because what the current array item has inside. }


What does array combine function do in php?

Creates an array using one value for keys and other for its values http://php.net/manual/en/function.array-combine.php


How can you mix up the order of values in a PHP array?

To shuffle an array in PHP is easy to do, all you need to use is the shuffle() function like shown below: &lt;?php $array = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); shuffle($array); // array results will be randomly shuffled ?&gt;


How do you find the size of an array in PHP?

To find the size of an array in PHP you can either use the count() function or the sizeof() function as they will produce the same result. &lt;?php $array = array(1, 2, 3, 4, 5, 6, 7); echo count($array); // outputs 7 echo sizeof($array); // outputs 7 ?&gt;


How do you pass a variable by value in php?

In the declaration of the receiving function, you add an ampersand. &lt;?php function myWayCoolFunction( &amp;$params) {.....} $x = array('1','2','3'); myWayCoolFunction($x) ?&gt;


In PHP 5.3.4 what is the value of array column?

array_column was not introduced into PHP until 5.5.0, so calling it in 5.3.4 would result in a fatal error, calling an undefined function.


How do you create an array in PHP?

The recommended function (a construct, really, but works as a function) would be array("value", "value"), which is much more backwards compatible. However the short syntax of ["value", "value"] is also available and frequently used, as it has been for some time.


How do you append post variable in an array in php?

The $_POST variable is an array already, so if you want to append it to another array, you can use the array_merge function to do so. For example:$foo = array('alpha', 'bravo', 'charlie');$bar = array_merge($foo, $_POST);If there were two variables posted, one called "blah" with a value of 7 and one called "snarf" with a value of "snoo", then the $bar array would now be:Array(0 => 'alpha',1 => 'bravo',2 => 'charlie','blah' => 7,'snarf' => 'snoo')


What is the difference between sort and asort in PHP?

sort() will order the array by its values without preserving the keys. Use it when array is indexed numerically or when you do not care about the keys. asort() will also sort the array by its values, but it will preserve the key -&gt; value association.