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

前端 未结 5 1978
予麋鹿
予麋鹿 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: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);
    }
    }

提交回复
热议问题