【问题标题】:Remove 1 row from array [duplicate]从数组中删除 1 行 [重复]
【发布时间】:2014-09-05 08:42:44
【问题描述】:

我有:

array(2) { 
    [0]=> array(23) { 
        ["data_cerere"]=> string(10) "2014-07-15" 
        ["nr_cerere_oferta"]=> string(13) "{49291-39958}" 
        ["clientul"]=> string(13) "Mihaela Curca" 
        ["client_email"]=> string(24) "aaaa@yahoo.com"  
    } 
    [1]=> array(23) { 
        ["data_cerere"]=> string(10) "2014-07-15" 
        ["nr_cerere_oferta"]=> string(13) "{76351-31554}" 
        ["clientul"]=> string(13) "Anca PLAVETIU" 
        ["client_email"]=>   string(28) "bbbbb@yahoo.com"  
    }        
}

我想检查数组中的"nr_cerere_oferta == x",如果找到则删除该行。

在这种情况下,"nr_cerere_oferta == {76351-31554}" 数组将是:

array(2) { 
    [0]=> array(23) { 
        ["data_cerere"]=> string(10) "2014-07-15" 
        ["nr_cerere_oferta"]=> string(13) "{49291-39958}" 
        ["clientul"]=> string(13) "Mihaela Curca" 
        ["client_email"]=> string(24) "aaaa@yahoo.com"  
    } 
}

【问题讨论】:

    标签: php arrays


    【解决方案1】:

    循环您的数组并检查该参数是否等于某个数字。

    foreach ($myArray as $i => $row) {
         if (strcmp($row['nr_cerere_oferta'], '{76351-31554}') === 0) {
              $myNewArray[] = $row; // UPDATED: add element to new array before delete
              unset($myArray[$i]);
         }
    }
    

    【讨论】:

    • 完美运行,感谢快速回放。
    • 我还有一个问题 .. 如何将所有已删除的元素添加到不同的数组中?
    • @user3679768 检查更新的答案
    【解决方案2】:

    您可以通过创建一个函数来使用array_map,在这种情况下可以像这样;

    function strip_element($elem) {
        if (isset($elem['nr_cerere_oferta'])) {
            unset($elem['nr_cerere_oferta']);
        }
        return $elem;
    }
    

    然后你将数组映射为;

    $strip = array_map('strip_element', $array);
    

    这将使您的输出没有您要删除的行。

    我使用的完整测试代码在这里;

    <?php
    $array = array
    ( 
        0 => array
        ( 
            "data_cerere"       => "2014-07-15",
            "nr_cerere_oferta"  => "{49291-39958}",
            "clientul"          => "Mihaela Curca",
            "client_email"      => "aaaa@yahoo.com"
        ),
        1 => array
        ( 
            "data_cerere"       => "2014-07-15",
            "nr_cerere_oferta"  => "{76351-31554}",
            "clientul"          => "Anca PLAVETIU",
            "client_email"      => "bbbbb@yahoo.com"
        )
    );
    
    echo '<pre>';
    print_r($array);
    echo '</pre>';
    
    function strip_element($elem) {
        if (isset($elem['nr_cerere_oferta'])) {
            unset($elem['nr_cerere_oferta']);
        }
        return $elem;
    }
    
    $strip = array_map('strip_element', $array);
    
    echo '<pre>';
    print_r($strip);
    echo '</pre>';
    

    以上测试代码的输出;

    Array
    (
        [0] => Array
            (
                [data_cerere] => 2014-07-15
                [nr_cerere_oferta] => {49291-39958}
                [clientul] => Mihaela Curca
                [client_email] => aaaa@yahoo.com
            )
    
        [1] => Array
            (
                [data_cerere] => 2014-07-15
                [nr_cerere_oferta] => {76351-31554}
                [clientul] => Anca PLAVETIU
                [client_email] => bbbbb@yahoo.com
            )
    
    )
    Array
    (
        [0] => Array
            (
                [data_cerere] => 2014-07-15
                [clientul] => Mihaela Curca
                [client_email] => aaaa@yahoo.com
            )
    
        [1] => Array
            (
                [data_cerere] => 2014-07-15
                [clientul] => Anca PLAVETIU
                [client_email] => bbbbb@yahoo.com
            )
    
    )
    

    【讨论】:

      【解决方案3】:

      创建一个类似的函数

      function array_row_copier($row_array){
          if($row_array["nr_cerere_oferta"] == "x")
              return false;
          else
              return true;
      }
      

      在这个函数之后用一个 for 循环检查数组,如果这个函数返回 true,则将数组复制到一个新的数组变量中

      【讨论】:

        猜你喜欢
        • 2010-12-19
        • 1970-01-01
        • 2011-06-29
        • 2014-07-20
        • 2020-02-08
        • 2019-10-19
        • 2011-01-04
        相关资源
        最近更新 更多