I have array like this :
$array = ["C","D","D#m","B","A","Am","A#m"];
How can I check my array items has this "#"?
Might be faster than a loop. Just create a string and check:
if(strpos(implode($array), '#') !== false) {
//yes
}
Or with a regex to check for any:
if(preg_grep('/#/', $array)) {
//yes
}
To get a count of the items:
if(count(preg_grep('/#/', $array)) == 3) {
//yes
}
Use below snippet of strpos to see if it exists '#',
foreach($arr as $v){
if(strpos($v, "#") !== false){
// do something
}
}
Demo.
Related
I need one help. I need to check that string is present inside array or not and also it should search letter wise using PHP. I am explaining my code below.
$resultArr=array("9937229853","9937229856","9937229875");
$searchValue="+919937229853";
Here I need to check that some of the value from $searchValue is present inside in array or not. I am doing like below but its not giving me the proper result.
$searchValue="+919937229853";
$resultArr=array("9937229853","9937229856","9937229875");
if(!in_array($searchValue, $resultArr))
{
$flag=1;
}else{
$flag=0;
}
echo $flag;
As per my requirement here result should print 1 because some value from $searchValue also present in that array but the echo result is coming 0.Please help me.
$searchValue="+919937229853";
$resultArr=array("9937229853","9937229856","9937229875");
foreach($resultArr as $value)
{
if(strpos($value,$searchValue) || strpos($searchValue,$value) || $searchValue==$value)
{
$flag = 1;
break;
}
else
$flag=0;
}
echo $flag;
I think this will do what you are looking for. in_array() method search string in array but for the exact string. strpos can search substring in long string and return the offset number or the index of substring in the long string.
you can try code like below.
if(!in_array(substr($searchValue,-10), $resultArr))
$searchValue="+919937229853";
$searchValue = str_replace("+91","",$searchValue);
$resultArr=array("9937229853","9937229856","9937229875");
if(in_array($searchValue, $resultArr))
{
$flag=1;
}else{
$flag=0;
}
echo $flag;
User str_replace function replace first three charater from string
$flag=0;
for($i=0;$i<strcmp($searchValue);$i++){
for($j=0;$j<strcmp($searchValue);$j++){
$part=substr($searchValue,$i,$j)
array_filter($resultArr, function($el) use ($part) {
if ( strpos($el, $part) !== false ){
$flag=1;
}
});
}
}
The function below will return true if either
your $searchValue is in the array (in_array), or
if any item of the array is a substring of $searchValue
in other words: If part of $searchValue is in the array.
This is the code and how you call it:
function search($needle, $haystack) {
// is $needle contained in the array as it is?
if (in_array($needle, $haystack))
return true;
// Is any part of $needle part of the array?
foreach ($haystack as $value) {
if (strpos($needle, $value) !== FALSE)
return true;
}
return false;
}
$resultArr = array("9937229853", "9937229856", "9937229875");
$searchValue = "+919937229853";
$result = search($searchValue, $resultArr);
var_dump($result);
I have this code:
$arr = array("Hello_backup","World!","Beautiful_backup","Day!");
if(in_array("backup", $arr)){
echo "Da";
} else { echo "Nu";
}
But is not working because,in_array instruction check the array for the complete string "backup" , which doesnt exist.I need to check for a part of the string,for example,to return true because backup is a part of the "Hello_backup" and "Beautiful_backup" strings
EDIT: I take the advice and i have used stripos like this:
$arr = array("Hello_backup-2014","World!","Beautiful_backup-2014","Day!");
$word='backup';
if(stripos($arr,$word) !== false){
echo "Da";
} else { echo "Nu";}
but now i get an error: "stripos() expects parameter 1 to be string, array given in if(stripos($arr,$word) !== false){"
Use implode to basically concatenate the array values as a string, then use strpos to check for a string within a string.
The first argument you pass to implode is used to separate each value in the array.
$array = array("Hello_backup","World!","Beautiful_backup","Day!");
$r = implode(" ", $array);
if (strpos($r, "backup") !== false) {
echo "found";
}
In this case you need to use stripos(). Example:
$arr = array("Hello_backup","World!","Beautiful_backup","Day!");
$needle = 'backup';
function check($haystack, $needle) {
foreach($haystack as $word) {
if(stripos($word, $needle) !== false) {
return 'Da!'; // if found
}
}
return 'Nu'; // if not found
}
var_dump(check($arr, $needle));
Without a function:
$arr = array("Hello_backup","World!","Beautiful_backup","Day!");
$found = false;
foreach($arr as $word) {
if(stripos($word, 'backup') !== false) {
$found = true;
break;
}
}
if($found) {
echo 'Da!';
} else {
echo 'Nu';
}
Try with strpos()
$arr = array("Hello_backup","World!","Beautiful_backup","Day!");
foreach($arr as $v){
echo (strpos($v,"backup")!== false ? "Da" : "Nu");
}
output :- DaNuDaNu
Here is the one line solution for you.
$arr = array("Hello_backup-2014","World!","Beautiful_backup-2014","Day!");
$returned_a = array_map(function($u){ if(stripos($u,'backup') !== false) return "Da"; else return "Nu";}, $arr);
You can use $returned_a with array as your answer..
Array ( [0] => Da [1] => Nu [2] => Da [3] => Nu )
Use this method. It is little bit simple to use.
$matches = preg_grep('/backup/', $arr);
$keys = array_keys($matches);
print_r($matches);
Look this working example
According to your question
$matches = preg_grep('/backup/', $arr);
$keys = array_keys($matches);
$matches = trim($matches);
if($matches != '')
{echo "Da";
}else { echo "Nu";}
<?php
$arr = array("Hello_backup","World!","Beautiful_backup","Day!");
foreach($arr as $arr1) {
if (strpos ($arr1,"backup")) {
echo "Da";
} else {
echo "Nu";
}
}
?>
I am trying to check for values in array and if value found increment it. I've tried to do it as shown in code below, but not successful.
$productdas=array("DAS","DayEnd","DAAS");
if (strpos(serialize($row['pirority']),"P1")!==false &&
strpos(serialize($row['product']),'$productdas')!==false)
{
$dasp1++;
}
I'll be grateful for any help.
Regards.
You would need to write an strpos() variant that accepts an array as $needle; for example:
function strpos_array($haystack, array $needles)
{
foreach ($needles as $needle) {
if (($pos = strpos($haystack, $needle)) !== false) {
return $pos;
}
}
return false;
}
if (strpos_array(serialize($row['product']), $productdas)!==false) { ... }
It's also possible to implement this using preg_match().
I'm assuming you're trying to search the given string for any of the values in $productas array and find the number of total occurrences. This can be done with substr_count():
$productdas = array("DAS","DayEnd","DAAS");
$count = 0;
foreach ($productdas as $needle) {
$count += substr_count($row['pirority'], $needle);
}
echo $count;
If $row['pirority'] is DASfooDayEndDAAShelloDAS, then the count would be 4.
Demo
I want to return index of the array. I am trying the following below but its not working:
$os = array("Helo how are you","I am you");
foreach ($os as $oss) {
if(strpos($oss, 'you') !== false) {
$reference= array_search('you', $oss);
echo $reference;
}
}
I would want the reference to echo 0, 1. given that I am searching for a word 'you' and it occurs in 0, 1. Is array search for a single string? What can do to search in the whole of substring?
array_search() wouldn't work here as it can't search for partial matches. Instead, you can use the following approach: if the strpos() statement evaluates to true, simply display the index for that iteration:
foreach ($os as $reference => $oss)
{
if(strpos($oss, 'you') !== false) {
echo $reference, PHP_EOL;
}
}
Output:
0
1
Demo
Try changing your foreach loop to this:
foreach ($os as $index => $oss)
{
if(strpos($oss, 'you') !== false) {
$reference= array_search('you', $oss);
echo $index;
}
}
I have an associative array like this:
9584=>string
5324=>string
6543=>string
The key is always a number but I assign it dynamically so I don't know the numbers and probably they are not consecutive.
I need to know if the string is the same in ALL of the occurrence in the array.
If you can help me thank you... and sorry for my horrible English
Let me count the ways... There are bound to be more:
if(count(array_flip($array)) === 1) { }
if(count(array_unique($array)) === 1) { }
if(count(array_count_values($array)) === 1) { }
Read the first value and browse your array until you find a different one.
<?php
function allTheSame($array)
{
if (count($array) != 0)
{
$first = reset($array);
foreach($array => $v)
{
if ($v !== $first)
{
return false;
}
}
}
return true;
}