i have 2 arrays
$filtered_url_list= array("www.salmat.", "www.webcentral.");
and
$files = array("http://www.salmat.", "http://www.webcentral.", "http://abc.");
i want to search for elements of $filtered_url_list in array $files. If part of the string of $files' elements match then respective $files' string needs to be echoed
my code looks like this
foreach($filtered_url_list as $check_val)
{
$found=FALSE;
foreach($files as $file_val)
{
if(stristr($check_val,$file_val)!==FALSE)
{
$found=TRUE;
}
}
if(!$found)
{
echo $file_val,"\n";
}
}
Example :
www.salmat. is present in http://www.salmat. if this is true then echo http://www.salmat.
My echo statement is incorrect. But i m not getting how to make it correct
Please suggest
Thankyou
Put the echo in the foreach. Also, the order of the arguments for the stristr function is wrong.
foreach($filtered_url_list as $check_val)
{
$found=FALSE;
foreach($files as $file_val)
{
if(stristr($file_val,$check_val)!==FALSE)
{
$found=TRUE;
echo $file_val,"\n";
}
}
}
Try this code:-
foreach($filtered_url_list as $check_val)
{
if(in_array ($check_val ,$contents))
{
//$chcekc_val values is present in $contents array.
}
}
For reference http://php.net/manual/en/function.in-array.php
Use strcmp instead
if(strcmp($check_val,$file_val) ==0)
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 array of objects:
$table=[{"count":"2","id_f":"2255"},{"count":"6","id_f":"5886"}];
I want to get the value of id_f of each object and check if this value exist in another array ,I tried with this but it gives me the wrong result:
foreach($table as $t){
if (in_array($t[$id_f],$array){
//dosomething}
}else{
//do something else
}
}
I also tried with this:
foreach($table as $t){
if (in_array($t->$id_f,$array){
//dosomething}
}else{
//do something else
}
}
I can't get the right result , I will appreciate any help.
Another approach without foreach loop:
<?php
$table=json_decode('[{"count":"2","id_f":"2255"},{"count":"6","id_f":"5886"}]');
$data = [10, 20, 2255];
array_walk($table, function($obj) use (&$data) {
if (in_array($obj->id_f, $data)) {
echo "+";
} else {
echo "-";
}
});
The output obviously is:
+-
You dont show a json_decode() anywhere in your code, thats the first thing to do with a JSON String, to decode it into a PHP data structure. In this case an array of objects.
$other_array = array('2255', '9999');
$table='[{"count":"2","id_f":"2255"},{"count":"6","id_f":"5886"}]';
$array = json_decode($table);
foreach ( $array as $obj ) {
if (in_array($obj->id_f, $other_array)) {
echo 'Found one ' . $obj->id_f . PHP_EOL;
} else {
echo 'No match for ' . $obj->id_f . PHP_EOL;
}
}
Results
Found one 2255
No match for 5886
There's no need for the dollar sign before your Object property name (actually, it won't work, except of course if $id_f is a real variable which has for value 'id_f', but somehow I doubt it) :
foreach ($table as $t) {
if (in_array($t->id_f, $array){
// do something
} else {
// do something else
}
}
it can be done like this:
to define the object array you can define like below with json string approach. or to define object is like this $table = new stdClass();
<?php
$table='[{"count":"2","id_f":"2255"},{"count":"6","id_f":"5886"}]';
$obj = json_decode($table);
$array=array("2555","2255");
foreach($obj as $t){
if (in_array($t->id_f,$array)){
//dosomething
}else{
//do something else
}
}
?>
The parameter is taken from the url like this file.php?item=xyz
I have an array $_SESSION['arr'].
Now I want to check if xyz is present in $_SESSION['arr'] and delete it if it is present.
In the end I send my result to the calling JQuery function as
echo json_encode($_SESSION['arr']);
EDIT: And if the element is not present, add it to the array.
Thanks in advance!
This should work for you :)
foreach ($_SESSION['arr'] as $value)
{
//converting to lowercase
$value= strtolower($value)
if($value == "xyz")
{
//removing $value from $_SESSION['arr']
unset($_SESSION['arr'][$value]);
}
else
{
//adding value to array
array_push($_SESSION['arr'], "xzy");
}
}
Use below code.
if(isset($_GET['item']) && $_GET['item'] == "xyz")
{
if(in_array($_GET['item'],$_SESSION['arr']))
{
//Delete it
}
}
if(isset($_GET['item']))
{
if(in_array($_GET['item'],$_SESSION['arr']))
{
$pos = array_search($_GET['item'], $_SESSION['arr']);
unset($_SESSION['arr'][$pos]);// deletes
}
not tested but logic like this. hope helps :)
I have an array where each element has a subarray with multiple Ids. When looping through the array, I'd like to check if the subarray has any elements besides a given one.
For example, I'd like to echo 'Yes' whenever one of the subarrays has any ids other than 'TESTID'.
I can do this by looping through the subarray, but I'd like to know of a way that doesn't require double loops.
Here's the current code:
foreach ($elements as $element) {
...
if (besidesInArray('TESTID',$element['ids'])) {
//operations
} else {
//element only has 'TESTID'
}
...
}
...
function besidesInArray($needle, $haystack) {
foreach ($haystack as $hay) {
if($hay != $needle) {
return TRUE;
}
}
return FALSE;
}
While this code works, I'd like to see if there's a more elegant solution.
You can use in_array() function to achieve this
foreach($array as $key => $subarray)
{
if(in_array("TESTID", $subarray))
{
//found
} else {
//not found
}
}
preg_grep for TESTID but invert the grep so that it returns entries NOT matching.
foreach($array as $subarray) {
if(preg_grep("/TESTID/", $subarray, PREG_GREP_INVERT)) {
echo 'Yes'; //others found
}
}
TESTID could be a var instead. Man I love some preg_grep!
find = implode(')|(',$mypatternarray);
find.="(".find.")";
foreach($subarray as $subar){
if(preg_match("find",$subar)>0)){
echo "id found";
}
}
I'm trying to check if the following is empty or not.
{"players":""}
I have a function that gets that from an api/site and.. well, heres the code.
function getPlayers($server) {
// Fetches content from url and json_decodes it
$playersList = getUrl('http://api.iamphoenix.me/list/?server_ip=' . $server);
// Attempting to check if it's empty.
if ($playersList != "") {
// convert list of comma-separated names into array
$players = explode(',', $playersList->players);
foreach ($players as $player) {
echo '<img title="'.$player.'" src="https://minotar.net/avatar/'.$player.'/32">';
}
} else {
return 'empty';
}
}
However, using !=, empty(), or isset(), I still get an empty string, example:
https://minotar.net/avatar//32
Where it should be..
https://minotar.net/avatar/Notch/32
When it's empty, I'd like it to just return 'empty'.
I'm not sure what I'm doing wrong. Any ideas?
In pure php you can check the url segments like
$_SERVER['REQUEST_URI_PATH'] = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$segments = explode('/', $_SERVER['REQUEST_URI_PATH']);
if($segments[2] == '') {
}
//or
if(empty($segments[2])) {
}
//or do your condition
if you are using codeigniter you might say
if(empty($this->uri->segment(2)))
But be sure you loaded the url helper
Hope I understand your question!
Since you were able to have some output, see my changes in the codes.
function getPlayers($server) {
// Fetches content from url and json_decodes it
$playersList = getUrl('http://api.iamphoenix.me/list/?server_ip=' . $server);
// Attempting to check if it's empty.
if ($playersList != "") {
// convert list of comma-separated names into array
$players = explode(',', $playersList->players);
// check conversion
if(is_array($players) && !empty($players){
foreach ($players as $player) {
echo '<img title="'.$player.'" src="https://minotar.net/avatar/'.$player.'/32">';
}
} else {
return 'empty';
}
} else {
return 'empty';
}
}
You should do this;
print_r($playersList);
just after you set it to see what it actually is. My guess is that you are not getting what you suspect from the getURL call.
Add one more equals sign to take type comparison into account as well
if ($playerList !== '')
try this
if (isset($playersList) && is_array($playersList) && !empty($playersList)) {
// convert list of comma-separated names into array
$players = explode(',', $playersList->players);
foreach ($players as $player) {
echo '<img title="'.$player.'" src="https://minotar.net/avatar/'.$player.'/32">';
}
} else {
return 'empty';
}