PHP Lesson 7 Usage of Arrays 2
Learning Outline:
1. Understand array functions
2. Randomly output verification code
1. Array function:
Array function:
//Function: Provides a lot of very useful code snippets written by the official to improve writing speed.
1. Array key value operation function
2. Count the elements and uniqueness of the array
3. Functions that use callback functions to process arrays
4. Array sorting function
5. Split, merge, decompose and combine functions
6. Arrays and data structures
7. Other useful array processing functions
Array key value operation function:
1.array_values();
Simulate getting the values of key and value
<?php $arr=array("name"=>"user1","age"=>"30","sex"=>"man"); foreach($arr as $key=>$val){ $keys[]=$key; $vals[]=$val; } echo "<pre class="code">"; print_r($keys); echo ""; echo "
"; echo "
"; print_r($vals); echo ""; ?>
2.Usage of array_values
<?php $arr=array("name"=>"user1","age"=>"30","sex"=>"man"); $keys=array_values($arr); echo "<pre class="code">"; print_r($keys); echo ""; ?>
array_values();//Get the values in the array
array_keys();//Get the keys in the array
in_array();//Check whether a value is in the array
array_key_exists();//Check whether a key is in the array
array_flip(); //Key and value swap
array_reverse();Reverse the values in the array
Count the elements and uniqueness of arrays
1.count();
2.array_count_values();//Count the number of occurrences of each value in the array.
3.array_unique();//Delete duplicates in the array
Functions that use callback functions to process arrays:
1.array_filter();
<?php $arr=array("user1"=>70,60,80,78,34,34,34,56,78,78); function older($var){ return ($var>60); } $arr2=array_filter($arr,"older"); echo "<pre class="code">"; print_r($arr2); echo ""; ?>
2.array_map();
Reference parameters:
Requirement: Array value increases by 1
function show(&$arr){
foreach($arr as $key=>$val){
$arr[$key]=$val+1;
}
}
Array sorting function
1.sort(); Ascending order, key is not retained
2.rsort(); Descending order, key is not retained
3.asort(); Ascending order, keep key
4.arsort(); Descending order, keep key
5.ksort(); Sort according to key in ascending order
6.krsort(); Sort by key in descending order
7.natsort(); natural number sorting in ascending order, such as the picture img2.jpg
8.natcasesort(); ignore case and sort in ascending order
9.multisort();Multiple array sorting
ksort();
<?php $arr=array("user1"=>10,"b"=>1,"c"=>3,"d"=>30); $arr2=array_flip($arr); ksort($arr2); echo "<pre class="code">"; print_r($arr2); echo ""; ?>
natsort();
<?php $array1 = $array2 = array("img12.png", "img10.png", "img2.png", "img1.png"); sort($array1); echo "Standard sorting\n"; print_r($array1); natsort($array2); echo "\nNatural order sorting\n"; print_r($array2); ?>
Multi-array sorting:
<?php $arr=array("aaa","bbbbbbbbb","cc","ddddd"); //需求: //1.按照标题长度排序 //2.标题长度变成标题字符串的key //将数组中的value的长度取出,并作为一个新数组 //strlen($val)取出字符串的长度 foreach ($arr as $val) { $lens[]=strlen($val); } array_multisort($lens,SORT_ASC,$arr);//对数组进行排序,根据第一个数组来排序第二个数组 SORT_ASC表示升序排序 sort($lens); $arr2=array_combine($lens, $arr);//第一个数组作为第二个数组对应的key,返回一个新数组 echo "<pre class="code">"; print_r($arr2); echo ""; ?>
Split, merge, decompose and combine functions
1.explode();
2.inplode();//join();
3.array_slice(); Array interception
4.array_splice(); Clipping of array
5.array-merge(); merge multiple arrays
6.array_combine(); merge arrays, two arrays, the former array as key, the latter array as value
7.array_intersect(); Find the intersection of two arrays
8.array_diff(); Find the difference between two arrays, based on the first parameter
9.array_pop(); pops a value from the end and returns the popup value
10.array_push(); Push a value from the last position and return the number of elements
11.array_shift(); delete a value from the position before washing
12.array_unshift(); Push a value from the front position
<?php $str="php,js,html,ces,div"; $arr=explode(",",$str); echo "<pre class="code">"; print_r($arr); echo ""; ?>
2.inplode(); Combine arrays into strings
<?php $str="php,js,html,ces,div"; $arr=explode(",",$str); $str2=implode("-",$arr); echo "<pre class="code">"; print_r($str2); echo ""; ?>
<?php $str="php,js,html,ces,div"; $arr=explode(",",$str); $arr2=array_reverse($arr);//讲数组中的值进行倒序 $str2=implode("-",$arr2); echo "<pre class="code">"; print_r($str2); echo ""; ?>
array_slice();
<?php //截取总是从后往前截取 $arr = array("aa","bb","cc","dd","ee","ff","gg"); $arr2 = array_slice($arr, 0,2);//表示从0的位置截取2个 aa bb $arr3 = array_slice($arr, -3,2);//表示从后往前数到3的位置,开始截取2个//ee ff echo "<pre class="code">"; print_r($arr3); echo ""; ?>
不仅拆减,而且可以添加
<?php $arr = array("aa","bb","cc","dd","ee","ff","gg"); $arr2 = array_splice($arr, 0, 3, array("hh","ii","jj","kk"));//直接取原数组的值,并将原数组进行改变,原数组为取走以后剩下的值 echo "<pre class="code">"; print_r($arr2); echo ""; echo "
"; print_r($arr); echo ""; ?> array_merge(); "; print_r($arr); echo ""; ?>
Other useful array processing functions:
1.array_rand();//Randomly pick a key
2.range();//Get an array of a certain range
3.shuffle();//The function of shuffling the array
4.array_sum();//Calculate the sum of all people in the array (calculate the total score)
If you calculate the key sum of an array, you can use array_flip() to swap the key sum values of the array, and then you can calculate the key sum.
<?php $arr = array("aa","bb","cc","dd","ee","ff","gg"); //将原数组顺序随机打乱 shuffle($arr); //取出数组的前3个 $arr2= array_slice($arr, 0 , 3); echo "<pre class="code">"; print_r($arr2); echo ""; ?>
//Randomly output four characters verification code implementation:
<?php //取出1-9 a-z A-Z的数组 $a = range(1, 9); $b = range(a, z); $c = range(A, Z); //将3个数组合并 $d = array_merge($a,$b,$c); //将合并后的数组打乱 shuffle($d); //取合并后的前4位 $e = array_slice($d, 0, 4); //将$e数组变为字符串 $f = join("", $e); echo $f; ?>
Please indicate the source for reprinting: http://blog.csdn.net/junzaivip

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver CS6
Visual web development tools