Flipkart

Monday, October 3, 2011

Remove array Element based on key value PHP

/**
 * Remove array Element based on key value
 * @param $arr
 * @param $key
 * @return array
 */
function array_pop_by_key($arr, $key) {
    $array_keys = array_keys($arr);
    foreach($arr as $array_key => $value) {
        if($array_key == $key) {
            unset($arr[$key]);
        }
    }
    return $arr;

}

1 comment:

  1. I don't get why you used this line:
    $array_keys = array_keys($arr);

    unless you meant to write this:

    function array_pop_by_key($arr, $key) {
    $array_keys = array_keys($arr);
    foreach($arr as $array_keys => $value) {
    if($array_key == $key) {
    unset($arr[$key]);
    }
    }
    return $arr;

    }

    which I don't think would make sense , but I'm learning , I might be wrong.

    ReplyDelete