0

I have a very basic question about PHP, I could not solve it with other answers in stackoverflow.

I have an array like :

[0] => this one i need 1
[1] => this one i need 2
[2] => not need this one

so, I want to check for every one, if it contains 'this one i need', then put that into another array

So we must have this array at least :

[0] => this one i need 1
[1] => this one i need 2

I tried to do it like this but it returned Empty array :

foreach($one as $value) {
   if(in_array("my name",$value)) $ok[] = $value;
}
3
  • 1
    Just to clarify you want to loop through all elements of the array, and if an element = 'XYZ' add to another array? Commented Aug 20, 2014 at 10:40
  • 1
    @buzzlightyear yeah, IF contains one string add to another array. Not equal to ! Commented Aug 20, 2014 at 10:42
  • 1
    Based on your code, $value should be array since you are using it as an array parameter in in_array() function, but based on foreach(), it should not be an array. So which is it? Commented Aug 20, 2014 at 10:47

3 Answers 3

2

try this

<?php

$one = array();

$one[0] = "this one i need 1";
$one[1] = "this one i need 2";
$one[2] = "not need this one";

$ok = array();
$find_str = "this one i need";
foreach($one as $value) {
       if(strpos($value, $find_str) !==false) 
       {
         $ok[] = $value;
       }  
    }


    print_r($ok);
?>

OUTPUT :

Array
(
    [0] => this one i need 1
    [1] => this one i need 2
)

Demo

UPDATE 2 :

as OP's $value is an array

$ok = array();
$find_str = "this one i need";
foreach($one as $value) {
   foreach($value as $val){
       if(strpos($val, $find_str) !==false) 
       {
         $ok[] = $value;
       }  
    }
 }   

    print_r($ok);
Sign up to request clarification or add additional context in comments.

6 Comments

Error : Warning: strpos() expects parameter 1 to be string, array given in
is $value is your array? please put your input array here
@Pafo try update 2 if $value is your array and this have values shown in question.
Tnx, i tried update two, i have very very big list, every array contains all of values :D
I am trying with all answers but i still giving that error !
|
1

The built-in array_filter() function is intended precisely for this purpose

$needle = 'XYZ';
$newArray = array_filter(
    $originalArray,
    function ($value) use ($needle) {
        return (strpos($value, $needle) !== false);
    }
);

Comments

1

array_filter is the perfect tool to delete unwanted elements based on values

$one[0] = "this one i need 1";
$one[1] = "this one i need 2";
$one[2] = "not need this one";

$wanted_string = 'this one i need';

$array_out = array_filter($one, function($var) use($wanted_string) {
            return strpos($var, $wanted_string) !== false;
});

// array(2) { [0]=> string(17) "this one i need 1" [1]=> string(17) "this one i need 2" } 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.