从数组中删除1行

时间:2014-07-15 08:28:04

标签: php arrays

我有:

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"  
    } 
}

3 个答案:

答案 0 :(得分: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]);
     }
}

答案 1 :(得分:0)

您可以通过创建一个函数来使用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
        )

)

答案 2 :(得分:-2)

创建一个像

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

在带有for循环的此函数之后检查数组,如果此函数返回true,则将数组复制到新的数组变量