Unset array che non include il valore dato nell'array multidimensionale [duplicato]

Oct 11 2020

Ho un array multidimensionale $sub_objects. Attualmente lo script seguente disattiva le chiavi che contengono "Apples". Invece voglio fare l'opposto. Desidero annullare l'impostazione delle chiavi che NON hanno il valore "Mele". Ho provato a impostare if(strpos($value, 'Apples') !== false)ma non ha funzionato. Come posso annullare l'impostazione dei valori che non hanno "Apple" invece di quelli che lo hanno? Come puoi vedere solo I like Green Eggs and Hamè rimasto nell'output, ma questo è l'unico che dovrebbe essere disattivato. I primi 3 dovrebbero rimanere, ma il 4 ° dovrebbe essere non impostato / cancellato.

Grazie!

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

produzione:

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

)

Risposte

Al-Amin Oct 11 2020 at 14:53

Prova questo, fai attenzione quando usi la PHP strposfunzione in quanto fornisce la posizione di occorrenza della stringa. Se trova la posizione sugli indici 0, verrà considerata falsa anche quando si utilizza la documentazione strpos== dell'operatore

<?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 );
ariefbayu Oct 11 2020 at 15:33

Propongo una soluzione lunga ma supponentemente più leggibile:

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

Il punto principale è usare array_filter () per filtrare l'indice indesiderato. Uso class per rendere il filtraggio più flessibile in quanto puoi fornire stringhe personalizzate al volo.

Ecco l'output di 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
    )

)
shingo Oct 11 2020 at 16:50

la risposta di jibsteroos è l'approccio comune, ma poiché questo è PHP, puoi ottenerlo con la magica sintassi continua

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