Home > Backend Development > PHP Tutorial > String array, delete array elements_PHP tutorial

String array, delete array elements_PHP tutorial

WBOY
Release: 2016-07-13 17:24:59
Original
971 people have browsed it

String array
Anyone who has studied C language may know that C language has strong string processing capabilities. PHP is written in C, so it naturally inherits C’s advantages in string processing. But after all, PHP is a new language, and it is still different from the C language. Naturally, there is no guarantee that it is exactly the same as C. So some features can only be known through testing.

Sometimes it is necessary to process each character of a string. The general approach may be:

$str="something";
for($i=0; $ i{
$ch=substr($str, $i, 1);
//Process $ch
}

This works, but is there a more elegant way? Yes, just treat it as an array, which is how C language handles it.

We change the above example to use a string array:

$str="something";
for($i=0; $i{
$ch=$str[$i];
//Process $ch
}

Isn’t this much better.

Delete array elements
What should I do if I define an array and sometimes want to delete some of it? I saw an answer in www.phpbuilder.com, which is to use the unset() function. Let's do a test.

$a[]="a1";
$a[]="a2";
$a[]="a3";
for($i=0; $ i{
echo $a[$i] . "
";
}
unset($a[0]);
for($i=0; $i{
echo $a[$i] . "
";
}

What does it mean? First generate an array $a with three elements, display it, then delete the first one (subscript 0), and then display it. The result should be that there are two elements left in the array. But that’s not right! The answer is different from what we thought. Is it because unset() is not easy to use? If you think about it carefully, it turns out that unset($a[0]) deletes the first element, but when outputting, we still start from $i=0, which of course is wrong. PHP will not automatically adjust the subscript. So we have to use other methods:



$a[]="a1";
$a[]="a2";
$a[]="a3" ;
for($i=0; $i{
echo $a[$i] . "
";
}
unset($a[0]);
reset($a); //Return the array pointer to the first element
while(list($c, $d)=each($a))
{
echo $d . "
"; //$c is the array subscript
}

This is a general method of displaying an array, without considering the array. The bid was placed.

Note: sizeof() is used to return the number of arrays, the same as count().

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/532101.htmlTechArticleString Array People who have studied C language may know that C language has strong processing functions for strings. PHP is written in C, so it naturally inherits C's advantages in handling strings. But...
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