How to check if an array value exists?
Using the instruction if
?
if(isset($something['say']) && $something['say'] === 'bla') {
// do something
}
By the way, you are assigning a value with the key say
twice, hence your array will result in an array with only one value.
How to check whether an array is empty using PHP?
If you just need to check if there are ANY elements in the array, you can use either the array itself, due to PHP's loose typing, or - if you prefer a stricter approach - use count()
:
if (!$playerlist) {
// list is empty.
}
if (count($playerlist) === 0) {
// list is empty.
}
If you need to clean out empty values before checking (generally done to prevent explode
ing weird strings):
foreach ($playerlist as $key => $value) {
if (!strlen($value)) {
unset($playerlist[$key]);
}
}
if (!$playerlist) {
//empty array
}
How can I check if an array element exists?
You can use either the language construct isset
, or the function array_key_exists
.
isset
should be a bit faster (as it's not a function), but will return false if the element exists and has the value NULL
.
For example, considering this array :
$a = array(
123 => 'glop',
456 => null,
);
And those three tests, relying on isset
:
var_dump(isset($a[123]));
var_dump(isset($a[456]));
var_dump(isset($a[789]));
The first one will get you (the element exists, and is not null) :
boolean true
While the second one will get you (the element exists, but is null) :
boolean false
And the last one will get you (the element doesn't exist) :
boolean false
On the other hand, using array_key_exists
like this :
var_dump(array_key_exists(123, $a));
var_dump(array_key_exists(456, $a));
var_dump(array_key_exists(789, $a));
You'd get those outputs :
boolean true
boolean true
boolean false
Because, in the two first cases, the element exists -- even if it's null in the second case. And, of course, in the third case, it doesn't exist.
For situations such as yours, I generally use isset
, considering I'm never in the second case... But choosing which one to use is now up to you ;-)
For instance, your code could become something like this :
if (!isset(self::$instances[$instanceKey])) {
$instances[$instanceKey] = $theInstance;
}
How can I check if an array contains a specific value in php?
Use the in_array()
function.
$array = array('kitchen', 'bedroom', 'living_room', 'dining_room');
if (in_array('kitchen', $array)) {
echo 'this array contains kitchen';
}
php check if a specific value in array contains value bigger than 0
You can iterate on your array and stop it when you have what you search:
$contains = false;
foreach(json_decode($json_data, true) as $item){
if ($item['value']) {
$contains = true;
break;
}
}
You can achieve that in many ways. But that can help you to go further.
PHP Check if array element exists in any part of the string
Just loop the array containing the values, and check if they are found in the input string, using strpos
$colors = array("blue","red","white");
$string = "whitewash"; // I want this to be found in the array
foreach ( $colors as $c ) {
if ( strpos ( $string , $c ) !== FALSE ) {
echo "found";
}
}
You can wrap it in a function:
function findString($array, $string) {
foreach ( $array as $a ) {
if ( strpos ( $string , $a ) !== FALSE )
return true;
}
return false;
}
var_dump( findString ( $colors , "whitewash" ) ); // TRUE
Check if string contains a value in array
Try this.
$string = 'my domain name is website3.com';
foreach ($owned_urls as $url) {
//if (strstr($string, $url)) { // mine version
if (strpos($string, $url) !== FALSE) { // Yoshi version
echo "Match found";
return true;
}
}
echo "Not found!";
return false;
Use stristr() or stripos() if you want to check case-insensitive.
Check if string contains any value of an array?
Explode $value
and convert it into array and then use array_intersect()
function in php to check if the string does not contain the value of the array.Use the code below
<?php
$search=array("<",">","!=","<=",">=");
$value='name >= vivek ';
$array = explode(" ",$value);
$p = array_intersect($search,$array);
$errors = array_filter($p);
//Check if the string is not empty
if(!empty($errors)){
echo "The string contains an value of array";
}
else
{
echo "The string does not containe the value of an array";
}
?>
Test the code here http://sandbox.onlinephpfunctions.com/code/7e65faf808de77036a83e185050d0895553d8211
Hope this helps you
Find if an array has at least 1 of the values
You might want to use array_intersect()
$search_values = array('cat', 'horse', 'dog');
$results = array('cat', 'horse');
if ( count ( array_intersect($search_values, $results) ) > 0 ) {
echo 'BINGO';
} else {
echo 'NO MATCHES';
}
Search for PHP array element containing string
To find values that match your search criteria, you can use array_filter
function:
$example = array('An example','Another example','Last example');
$searchword = 'last';
$matches = array_filter($example, function($var) use ($searchword) { return preg_match("/\b$searchword\b/i", $var); });
Now $matches
array will contain only elements from your original array that contain word last (case-insensitive).
If you need to find keys of the values that match the criteria, then you need to loop over the array:
$example = array('An example','Another example','One Example','Last example');
$searchword = 'last';
$matches = array();
foreach($example as $k=>$v) {
if(preg_match("/\b$searchword\b/i", $v)) {
$matches[$k] = $v;
}
}
Now array $matches
contains key-value pairs from the original array where values contain (case- insensitive) word last.
Related Topics
Macd Function Returning Incorrect Values
How to Change the Name of an Element in Dom
PHP Type Hinting Not Getting Along with Interfaces and Abstract Classes
Zend Framework 2 - Removed Form Element Causes Validation to Fail
Nl2Br() Equivalent in JavaScript
Replace the Last Comma in a String Using Regular Expression
Nohup on Windows, Exec Without Waiting for Finish
What Is the Use of Strict in Laravel Config/Database
Http Referer Not Always Being Passed
What Does the "&" Sign Mean in PHP
Google Calendar API Batch Request PHP
How Does Codeigniter Receive the Ajax Post Data in Controller
PHP Xpath - Find Element with a Value and Also Get Elements Before and After Element
Strip_Tags() Function Blacklist Rather Than Whitelist
Creating a Secure File Hosting Server for PDFs