Basics of array application in PHP (5)

黄舟
Release: 2023-03-03 21:20:01
Original
1377 people have browsed it

1.The foreach loop statement traverses the two-dimensional array. This is a foreach traversal of a two-dimensional array.
$erwei_arr = array(
array("xiaoliu","123456″,"Xiao Liu","Male",29,"System Analyst") ,
array("xiaozeng","123456″,"Xiao Zeng" ,"Male",23,"Web Engineer") ,
array("xiaochen","123456″,"Xiaochen","Male",29,"Java Engineer")
); //Declare the index two-dimensional array

$html = “

”; //Define the string $html, the content of the string is the








tag of the tableforeach($erwei_arr as $ field){$html .= “ ”; //Use string operators to concatenate the original value of $html and “ ” into a new string, the same as belowforeach($field as $value){$html .= “ ”;}$html .= “ ”;}$html .= “
”.$value.”
”;

echo $html;

The result is

This is how PHP operates the table. hehe.

2. Commonly used array processing functions

Array processing functions are also more important, such as random functions, sorting functions, etc. Roughly:

Index/value operation function

in_array() checks whether a certain value exists in the array, array_search() searches for a given value in the array, and returns the corresponding key name if successful, array_key_exists() checks for Whether the specified key name exists in the array

Array sorting function

sort() sorts the array in ascending order by value and re-enumerates the array, rsort() is in descending order, which is the opposite. asort() sorts the array in ascending order by value, keeping the association between index and value, while arsort() does the opposite. ksort() sorts the array in ascending order by key name, keeping the association between index and value, while krsort() does the opposite.

Array Statistics/Unique Function

The count() function was also mentioned earlier. Its function is to count the number of array elements in the array.

Split, merge and decompose array functions

array_combine() and array_merge(), look at the code directly
$one_array = array("name","gender","age","position");
$two_array = array("Xiao Zeng","Male",23,"E-Commerce Lecturer");

echo "

The new array nc_array***** after *********array_combine() *****

”;
$nc_array = array_combine($one_array , $two_array);
print_r($nc_array);

The result is Array ( [name] => Xiao Zeng [gender] = > Male [Age] => 23 [Position] => E-commerce Lecturer)

echo “

The new array nm_array***** after *********array_merge() *****

”;
$nm_array = array_merge($one_array , $two_array);
print_r($nm_array);

The result is Array ([0] => Name[1] => ; Gender[2] => Age[3] => Position[4] => Xiao Zeng[5] => Male[6] => 23 [7] => E-commerce lecturer)
Random Function

This should be very common to everyone. Random articles, random advertisements, or lottery draws on some websites all use this principle in PHP.

The main functions are array_rand() and shuffle().

array_rand() randomly removes one or specified array elements from the array and returns an array containing random key names.

shuffle() is to randomly shuffle an array.

Here we are. Zhutou blog has been busy recently, so updates are unstable, haha, it doesn’t matter. I would like to state here that part of the code is based on the source code of my lecturer, Mr. Zeng Wenbing.

The above is the content of PHP array application basics (5). For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!


Related labels:
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!