【问题标题】:Unset array that does not include given value in multidimensional array [duplicate]在多维数组中不包含给定值的未设置数组[重复]
【发布时间】:2021-01-25 19:15:45
【问题描述】:

我有一个多维数组$sub_objects。目前,下面的脚本会取消设置包含“Apples”的键。相反,我想做相反的事情。我想取消设置没有值“Apples”的键。我尝试设置if(strpos($value, 'Apples') !== false),但这并没有做任何事情。如何取消设置没有“Apple”的值而不是那些有的值?如您所见,输出中只剩下I like Green Eggs and Ham,但这是唯一应该取消设置的。前 3 个应保留,但第 4 个应取消设置/删除。

谢谢!

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

输出:

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

)

【问题讨论】:

  • 让我们假设密钥可能不是文本,可能是苹果可能存在的其他东西。

标签: php


【解决方案1】:

试试这个,使用PHP strpos 函数时要小心,因为它给出了字符串出现的位置。如果它在 0 个索引上找到位置,那么当您使用 == 运算符 strpos documentation 时,它也将被视为 false

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

【讨论】:

    【解决方案2】:

    我提出了一个冗长但固执更具可读性的解决方案:

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

    主要的收获是使用array_filter() 过滤掉不需要的索引。我使用类来使过滤更加灵活,因为您可以即时提供自定义字符串。

    这是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
        )
    
    )
    

    【讨论】:

    • 如果你有一个以Apple开头的字符串,这不起作用,有一个look
    • @jibsteroos 正确,正在更新逻辑。
    【解决方案3】:

    jibsteroos 的回答是常用的方法,但是由于这是 PHP,你可以通过神奇的 continue 语法来实现它

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

    【讨论】:

      猜你喜欢
      • 2019-10-01
      • 2013-02-18
      • 2022-01-08
      • 2020-09-23
      • 2013-12-17
      • 2014-02-19
      • 2017-11-16
      • 2015-02-17
      • 1970-01-01
      相关资源
      最近更新 更多