-1

I have a multidimensional array $sub_objects. Currently the script below unsets the keys that have 'Apples' included. Instead I want to do the opposite. I want to unset keys that DO NOT have the value 'Apples'. I tried setting if(strpos($value, 'Apples') !== false) but that did not do anything. How can I unset values that do not have 'Apple' instead of those that do? As you can see only I like Green Eggs and Ham is left in the output, but this is the only one that should be unset. The first 3 should remain, but the 4th should be unset/deleted.

Thanks!

$sub_objects = [
    ['text' => 'I like Apples', 'id' => '102923'],
    ['text' => 'I like Apples and Bread', 'id' =>'283923'],
    ['text' => 'I like Apples, Bread, and Cheese', 'id' => '3384823'],
    ['text' => 'I like Green Eggs and Ham', 'id' =>'4473873']
];

foreach($sub_objects as $key => $array) {
    foreach($array as $value) {
        if(strpos($value, 'Apples') !== false) {
            //print_r($key);
            unset($sub_objects[$key]);
        } 
    }
}

print_r($sub_objects);

output:

Array
(
    [3] => Array
        (
            [text] => I like Green Eggs and Ham
            [id] => 4473873
        )

)
soniccool
  • 5,790
  • 22
  • 60
  • 98

3 Answers3

0

Try this, be careful when using the PHP strpos function as it gives the position of occurrence of the string. If it finds the position on 0 indexes then it will also count as false when you use == operator strpos documentation

<?php


$sub_objects = [
    ['text' => 'I like Apples', 'id' => '102923'],
    ['text' => 'I like Apples and Bread', 'id' =>'283923'],
    ['text' => 'I like Apples, Bread, and Cheese', 'id' => '3384823'],
    ['text' => 'I like Green Eggs and Ham', 'id' =>'4473873']
];

foreach($sub_objects as $key => $value ) {
        if( strpos( $value['text'] , 'Apples') === false ){
            unset( $sub_objects[$key] );
        }
}

var_dump( $sub_objects );
Rabby
  • 322
  • 4
  • 15
Al-Amin
  • 596
  • 3
  • 16
0

I propose a long but opinionatedly more readable solution:

$sub_objects = [
  ['text' => 'I like Apples', 'id' => '102923'],
  ['text' => 'I like Apples and Bread', 'id' => '283923'],
  ['text' => 'I like Apples, Bread, and Cheese', 'id' => '3384823'],
  ['text' => 'I like Green Eggs and Ham', 'id' => '4473873']
];

$sub_objects = array_filter($sub_objects, array(new FilterWord('Apple'), 'isContains'));

print_r($sub_objects);

class FilterWord
{
  private $word;

  function __construct($word)
  {
    $this->word = $word;
  }

  function isContains($string)
  {
    return !(strpos($string['text'], $this->word) === false);
  }
}

The main takeaway is to use array_filter() to filter out unwanted index. I use class to make filtering more flexible as you can supply custom string on the fly.

Here is the output of print_r():

Array
(
  [0] => Array
    (
      [text] => I like Apples
      [id] => 102923
    )

  [1] => Array
    (
      [text] => I like Apples and Bread
      [id] => 283923
    )

  [2] => Array
    (
      [text] => I like Apples, Bread, and Cheese
      [id] => 3384823
    )

)
ariefbayu
  • 21,849
  • 12
  • 71
  • 92
0

jibsteroos's answer is the common approach, but since this is PHP, you can achieve it by the magical continue syntax

foreach($sub_objects as $key => $array) {
    foreach($array as $value) {
        if(strpos($value, 'Apples') !== false)
            continue 2;
    }
    unset($sub_objects[$key]);
}
shingo
  • 18,436
  • 5
  • 23
  • 42