1

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

3 Answers 3

2

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

Try this code snippet here

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);
Sign up to request clarification or add additional context in comments.

Comments

0

you can use

unset(array[2]);

thank you

1 Comment

Whilst this code snippet is welcome, and may provide some help, it would be greatly improved if it included an explanation of how it addresses the question. Without that, your answer has much less educational value - remember that you are answering the question for readers in the future, not just the person asking now! Please edit your answer to add explanation, and give an indication of what limitations and assumptions apply.
0

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

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.