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

PHP method to delete specified array elements_PHP tutorial

WBOY
Release: 2016-07-20 11:02:58
Original
804 people have browsed it

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...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template