Is there a PHP function to remove any/all key/value pairs that have a certain value from an array?

前端 未结 5 1936
予麋鹿
予麋鹿 2021-01-26 11:27

I think questions like this are the reason why I don\'t like working with PHP. The manual is good, if you can find what you are looking for. After reading through the Array Func

相关标签:
5条回答
  • 2021-01-26 11:29

    array_filter does this for you. You just need to provide a filter callback function:

    function myFilter($Value){
       if($Value == 'red'){
          return false;
       }
       return true;
    }
    
    $Values = array("a" => "green", "red", "bicycle", "red");
    
    $Values = array_filter($Values, 'myFilter');
    

    returns:

    array {
      ["a"] => "green"
      [1]   => "bicycle"
    }
    

    The filter function should return true for values you want to keep and false for those you wish to remove. Then just go ahead and use array_values to re-index the array. e.g.

    $Values = array_values(array_filter($Values, 'myFilter'));
    

    If you are doing this within an object and you want to call a filter method within the object you can use this form for the callback:

    array_filter($Values, array($this,'myFilter'));
    
    0 讨论(0)
  • 2021-01-26 11:30

    I like the array_diff function, but I have my one scripted one if you dont want to pass down an array:

    function array_unset_value($value, &$array) {
        $key = array_search($value, $array);
        while ($key !== false) {
            unset($array[$key]);
            $key = array_search($value, $array);
        }
    }
    
    0 讨论(0)
  • 2021-01-26 11:45

    Just to add to this...

    array_diff appears to show elements in the first array which don't appear in the second array. It doesn't show those elements which only appear in one or the other array.

    e.g.

    <?
    $test = "hello";
    $array1 = array("a" => "green", "red", "bicycle", "red");
    $array2 = array("b" => "green", "yellow", "red", "blue", "yellow", "pink");
    $result = array_diff($array1, $array2);
    print_r ($result);
    ?> 
    

    returns

    Array
    (
        [1] => bicycle
    )
    
    0 讨论(0)
  • 2021-01-26 11:53

    array_diff is what you want.

    $array1 = array("a" => "green", "red", "blue", "red");
    $array2 = array("b" => "green", "yellow", "red");
    $result = array_diff($array1, $array2);
    

    Result: "blue".

    0 讨论(0)
  • 2021-01-26 11:53

    Rebuild this "impossible" function, I'd suggest :
    function pf_cleanarray($arraytoclean=false,$to_explode=false,$delimiter=false) { /* PortalPress.org 2011 [maintain this comment, please] - cleans up an array upto to NULL or makes an array with content; use with care and better not use on even length depending [associative] arrays


    It looks terrible, I know, especially without indent on Stackoverflow. But when rebuilding, it can be a very useful, dynamic tool, using just the simplest functions, so you will be able to manipulate keys and values at will on a multi-array. The keyfunction is to delete empty values or make an array with explode, that's clean, but with a little trouble, it can be made into a function that eliminates the elements depending on key or value. - Igor M. -

    Simple example:
    $A[0][0][0][0][0][0]="A";
    $A[1][0][0][0][0][0]="";
    $A[2][0][0][0][0][0]="";
    $A[0][0][0][0][0][1]="";
    $A[1][0][0][0][0][1]="";
    $A[2][0][0][0][0][1]="";
    $A[0][0][0][0][0][2]="";
    $A[1][0][0][0][0][2]="";
    $A[2][0][0][0][0][2]="";
    $A[0][0][0][0][0][3]="";
    $A[1][0][0][0][0][3]="";
    $A[2][0][0][0][0][3]="";
    $A[0][0][0][0][0][4]="";
    $A[1][0][0][0][0][4]="";
    $A[2][0][0][0][0][4]="";
    $A[0][0][0][0][0][5]="";
    $A[1][0][0][0][0][5]="A";
    $A[2][0][0][0][0][5]="";
    The $A array: Array([0]=>Array([0]=>Array([0]=>Array([0]=>Array([0]=>Array([0]=>A[1]=>[2]=>[3]=>[4]=>[5]=>)))))
    [1]=>Array([0]=>Array([0]=>Array([0]=>Array([0]=>Array([0]=>[1]=>[2]=>[3]=>[4]=>[5]=>A)))))
    [2]=>Array([0]=>Array([0]=>Array([0]=>Array([0]=>Array([0]=>[1]=>[2]=>[3]=>[4]=>[5]=>))))))
    Results in $A:
    Array([0]=>Array([0]=>Array([0]=>Array([0]=>Array([0]=>Array([0]=>A)))))
    [1]=>Array([0]=>Array([0]=>Array([0]=>Array([0]=>Array([0]=>A))))))
    */
    if($arraytoclean===0 || $arraytoclean) {
    if(!is_array($arraytoclean)) {
    if($to_explode && $delimiter) {
    $arraytoclean=explode($delimiter,$arraytoclean);
    } else {
    $arraytoclean=Array($arraytoclean);
    }
    }
    $nZx=0;
    $keyarray=array_keys($arraytoclean);
    for($nYx=0;$nYx $keydigit=true;
    if($keyarray[$nYx]!==$nYx) {
    $keydigit=false;
    break;
    }
    }
    for($nYx=0;$nYx if(!is_array($arraytoclean[$keyarray[$nYx]])) {
    if($arraytoclean[$keyarray[$nYx]]) {
    if(!isset($retarraytoclean)) {
    $retarraytoclean=Array();
    }
    $retarraytoclean[(($keydigit) ? $nZx : $keyarray[$nYx])]=$arraytoclean[$keyarray[$nYx]];
    $nZx++;
    }
    } else {
    $temparr=pf_cleanarray($arraytoclean[$keyarray[$nYx]]);
    if($temparr) {
    if(!isset($retarraytoclean)) {
    $retarraytoclean=Array();
    }
    $retarraytoclean[(($keydigit) ? $nZx : $keyarray[$nYx])]=$temparr;
    $nZx++;
    }
    }
    }
    }
    if(isset($retarraytoclean)) {
    return($retarraytoclean);
    } else {
    return(NULL);
    }
    }

    0 讨论(0)
提交回复
热议问题