如果数组中的每一个都包含一个字符串,则在PHP中推送到另一个数组

如果数组中的每一个都包含一个字符串,则在PHP中推送到另一个数组,php,arrays,Php,Arrays,我有一个关于PHP的非常基本的问题,我无法用stackoverflow中的其他答案来解决它 我有这样一个数组: [0] => this one i need 1 [1] => this one i need 2 [2] => not need this one 所以,我想检查每一个,如果它包含“ThisOneIneed”,然后将其放入另一个数组中 所以我们必须至少有这个数组: [0] => this one i need 1 [1] => this one i n

我有一个关于PHP的非常基本的问题,我无法用stackoverflow中的其他答案来解决它

我有这样一个数组:

[0] => this one i need 1
[1] => this one i need 2
[2] => not need this one
所以,我想检查每一个,如果它包含“ThisOneIneed”,然后将其放入另一个数组中

所以我们必须至少有这个数组:

[0] => this one i need 1
[1] => this one i need 2
我尝试这样做,但它返回空数组:

foreach($one as $value) {
   if(in_array("my name",$value)) $ok[] = $value;
}
试试这个

<?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);
?>

更新2:

as OP的
$value
是一个数组

$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);
试试这个

<?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);
?>

更新2:

as OP的
$value
是一个数组

$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);
内置功能正是为了实现这一目的

$needle = 'XYZ';
$newArray = array_filter(
    $originalArray,
    function ($value) use ($needle) {
        return (strpos($value, $needle) !== false);
    }
);
内置功能正是为了实现这一目的

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

array\u filter
是根据值删除不需要的元素的完美工具

$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" } 

array\u filter
是根据值删除不需要的元素的完美工具

$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" } 

只是为了澄清一下,您希望循环遍历数组的所有元素,如果元素='XYZ'添加到另一个数组?@buzzlightyear是的,如果包含一个字符串,则添加到另一个数组。不等于!根据您的代码,
$value
应该是数组,因为您在_array()函数中使用它作为数组参数,但根据
foreach()
,它不应该是数组。那么是哪一个呢?只是想澄清一下,你想循环数组的所有元素,如果一个元素='XYZ'添加到另一个数组?@buzzlightyear是的,如果包含一个字符串,则添加到另一个数组。不等于!根据您的代码,
$value
应该是数组,因为您在_array()函数中使用它作为数组参数,但根据
foreach()
,它不应该是数组。那么是哪一个呢?错误:警告:strpos()希望参数1是字符串,数组给定inis
$value
是您的数组吗?请把你的输入数组here@Pafo如果
$value
是您的数组,并且该数组中有问题显示的值,请尝试更新2。Tnx,我尝试了更新2,我有一个非常非常大的列表,每个数组都包含所有值:我正在尝试所有答案,但我仍然给出了错误!错误:警告:strpos()要求参数1为字符串,给定的数组inis
$value
是您的数组吗?请把你的输入数组here@Pafo如果
$value
是您的数组,并且该数组中有问题显示的值,请尝试更新2。Tnx,我尝试了更新2,我有一个非常非常大的列表,每个数组都包含所有值:我正在尝试所有答案,但我仍然给出了错误!