Home  >  Article  >  Backend Development  >  PHP method to delete specified array elements_PHP tutorial

PHP method to delete specified array elements_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:02:58782browse

Two efficient and classic methods for deleting array elements are provided below. We can delete the ones that we want to delete. Students in need can refer to them. Here are the methods from php.net. ​

The code is as follows Copy code
 代码如下 复制代码

function deleteFromArray(&$array, $deleteIt, $useOldKeys = FALSE)
{
    $tmpArray = array();
    $found = FALSE;
    foreach($array as $key => $value)
    {
        if($value !== $deleteIt)
        {
            if(FALSE === $useOldKeys)
            {
                $tmpArray[] = $value;
            }
            else
            {
                $tmpArray[$key] = $value;
            }
        }
        else
        {
            $found = TRUE;
        }
    }
  
    $array = $tmpArray;
  
    return $found;
}
?>

The code is as follows Copy code

function deleteFromArray(&$array, $deleteIt, $useOldKeys = FALSE)
{
$tmpArray = array();
$found = FALSE;
foreach($array as $key => $value)
{
If($value !== $deleteIt)
                                  {
                if(FALSE === $useOldKeys)
                                                        {
                      $tmpArray[] = $value;
             }
             else
                                                        {
                    $tmpArray[$key] = $value;
             }
         }
        else
                                  {
               $found = TRUE;
         }
}

$array = $tmpArray;

Return $found;
}
?>

http://www.bkjia.com/PHPjc/445321.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445321.htmlTechArticle
The following are two efficient and classic methods for deleting array elements. We can delete the one we want to delete if necessary. Students can refer to it, here is from php.net. The code is as follows...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn