Reputation: 109
Say I have a array like
Array
(
[0] => Array ( [name] => test1 [type] => this1 [location] => 1 )
[1] => Array ( [name] => test2 [type] => this2 [location] => 2 )
[2] => Array ( [name] => testing2 [type] => this3 [location] => 3 )
)
and I want to remove the the key where anywhere it the array it contains the word "testing" doesn't have to be a exact match as long as it has that word.
so ideally [2] should be removed/unset because it contains that word. How can I achieve this.
Expected output:
Array
(
[0] => Array ( [name] => test1 [type] => this1 [location] => 1 )
[1] => Array ( [name] => test2 [type] => this2 [location] => 2 )
)
Thank you
Upvotes: 1
Views: 43
Reputation: 1
this code will work for you
$search = "testing";
$b =array(array ( 'name' => "test1", 'type' => "this1", 'location' => 1 ) ,
array ( 'name' => "test2" ,'type' => "this2", 'location' => 2 ) ,
array ( 'name' => "testing2", 'type'=> "this3", 'location' => 3 ));
foreach ($b as $key=>$val) {
foreach($val as $key1=>$val1){
if (strpos($val1,$search) !== FALSE) {
unset($b[$key]);
break;
}
}
}
echo '<pre>';print_r($b);
Upvotes: 0
Reputation: 15141
Hope this will help you out.. Here we are using implode
and stristr
, we are joining an array into string using implode
and searching string using stristr
ini_set('display_errors', 1);
$rows=Array (
0 => Array ( "name" => "test1","type" => "this1", "location" => 1 ),
1 => Array ( "name" => "test2" ,"type" => "this2", "location" => 2 ),
2 =>Array ( "name" => "testing2","type" => "this3", "location" => 3 ));
$wordToSearch="testing";
foreach($rows as $key => $value)
{
if(stristr(implode("", $value), $wordToSearch))
{
unset($rows[$key]);
}
}
print_r($rows);
Upvotes: 2