0

I have:

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

I want to check the array for "nr_cerere_oferta == x" and if found to remove the row.

In this case "nr_cerere_oferta == {76351-31554}" the array will be:

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"  
    } 
}
Gwenc37
  • 2,064
  • 7
  • 18
  • 22

3 Answers3

1

Loop your array and check if that parameter is equal to some number.

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]);
     }
}
Justinas
  • 41,402
  • 5
  • 66
  • 96
0

You can use array_map by creating a function, which in this instance could like like;

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

Then you map the array with something such as;

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

Which would then leave your output without the row you wanted to delete.

Full test code I used is here;

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

Output from above test code;

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
        )

)
MrMarlow
  • 856
  • 4
  • 17
-2

create a function like

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

after this function with a for loop check the array and if this function returns true copy the array to a new array variable

BrokenFrog
  • 500
  • 1
  • 6
  • 15