Jump to content

Recommended Posts

hello i`m trying to loop over and over the arrays

and try to find some words

the thing is it doesnt work .. i wanna find even a piece of it

but its not working and cant understand why.....

 

 

$array=array('first','second','third','3423a','3423');
$c=count($array);
for($i=0;$i<$c;$i++){
$result=strpos($array[$i],'3423'); 	
if($result!==false){
echo 'found it';

}else{ 
echo 'NOT found it';

}


}

Link to comment
https://forums.phpfreaks.com/topic/141134-solved-find-piece-in-array/
Share on other sites

you're not finding it because first comes before second, and "sec" doesn't exist in "first" and either way if it matches or doesn't match, you die() the script.. take the die() out of the else statement and you should be okay..

 

<?php

$array=array('first','second','third','3423a','3423');

 

foreach($array as $arrays){

$result=strpos($arrays,'sec');

if($result!==false){

echo 'found it<br />';

die;

} else {

echo 'NOT found it<br />';

}

}

?>

ahh i modify it and figure about die;

 

russel i tried ur way... but it finds both ways

 

NOT found it

found it

 

$array=array('first','second','third','3423a','76676');

foreach($array as $arrays){
   $result=strpos($arrays,'76');    
   if($result!==false){
      echo 'found it<br />';
      die;
   } else { 
      echo 'NOT found it<br />';
   }
}

 

i`ve use it like this

 

edit : ive tried like that and it shows

i understand why but i dont know how to limit to only the valid one

 

NOT found it

NOT found it

NOT found it

NOT found it

found it

I figured you wanted it to show when it didn't find it..

 

remove the else statement all together

 

 

$array=array('first','second','third','3423a','76676');

 

foreach($array as $arrays){

  $result=strpos($arrays,'76');   

  if($result!==false){

      echo 'found it<br />';

      die;

  }

}

 

ahh i modify it and figure about die;

 

russel i tried ur way... but it finds both ways

 

NOT found it

found it

 

$array=array('first','second','third','3423a','76676');

foreach($array as $arrays){
   $result=strpos($arrays,'76');    
   if($result!==false){
      echo 'found it<br />';
      die;
   } else { 
      echo 'NOT found it<br />';
   }
}

 

i`ve use it like this

 

edit : ive tried like that and it shows

i understand why but i dont know how to limit to only the valid one

 

NOT found it

NOT found it

NOT found it

NOT found it

found it

 

Instead of die; you could just use break;

$array=array('first','second','third','3423a','76676');

foreach($array as $arrays){
   $result=strpos($arrays,'76');    
   if($result!==false){
      echo 'found it<br />';
      break;
   } 
}

echo ' Break allows for more processing here.';

 

thanks alot i see know

is there just one thing that i want to know if there`s possible

is there a way to echo just one value if it doesnt find any results... something like this

 

if($result===false){
   echo 'not found'; // loop through all the arrays and if no results in ALL of them just show that echo 
}

Create a boolean.

 

 

$array=array('first','second','third','3423a','76676');
$foundOne = FALSE;

foreach($array as $arrays){
   $result=strpos($arrays,'76');    
   if($result!==false){
      echo 'found it
';
      $foundOne = TRUE;
      break;
   } 
}

if($foundOne == FALSE)
   echo "You didn't find anything";

echo ' Break allows for more processing here.';

Edit:  Beaten to it, but the Post button is glaring at me :(.

 

 

I would probably wrap it in a function:

 

 

function InValue($array, $valpiece) {
    foreach($array as $val) {
        if(strpos($array, $valpiece) !== false) return true;
    }
    return false;
}
if(InValue($array, '76'))
    echo 'Found!';
else
    echo 'Not found .';

 

 

If you don't want it in a function:

 

$found = false;
foreach($array as $val) {
   if(strpos($array, $valpiece) !== false) {
       $found = true;
        break;
    }
}
if($found)
    echo 'Found!';
else
    echo 'Not found .';

thanks alot friends its works good

corbin u misspelled

 

function InValue($array, $valpiece) {
    foreach($array as $val) {
         // $val instead of array
        if(strpos($val, $valpiece) !== false) return true;
    }
    return false;
}
if(InValue($array, '76'))
    echo 'Found!';
else
    echo 'Not found .';


 

anyway thanks alot :D

edit : ahh  just instead of array

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.