Home Backend Development PHP Tutorial PHP array related explanations

PHP array related explanations

Aug 04, 2017 pm 05:48 PM
php Related explain

For web programming, the most important thing is accessing and reading and writing data. There may be many storage methods, which can be in the form of strings, arrays, files, etc.

Today I learned about arrays, which can be said to be one of the more important methods in PHP data applications. There are many array functions in PHP. The following is a summary of what I learned. I will remember it for future reference...
1. Array definition:
The definition of an array is defined using the array() method. , you can define an empty array:

Copy code The code is as follows:


$number = array(1,3 ,5,7,9);
//Define empty array
$result = array();
$color =array("red","blue","green");
//Custom key value
$language = (1=>"English",3=>"Chinese",5=>"Franch");
//Define a two-dimensional array
$two = array(
"color"=>array("red","blue"), //End with comma
"week"=>array("Monday","Friday") / /The last sentence has no punctuation
);
?>


2. Create an array:
The functions included in creating an array include compact( ),
 1.compact() function - convert one or more variables (including arrays) into an array:
  array compact (mixed $varname [, mixed $...] ) 

Copy code The code is as follows:


$number = "1,3,5,7,9";
$ string = "I'm PHPer";
$array = array("And","You?");
$newArray = compact("number","string","array");
print_r ($newArray);
?>


compact() function is used to convert two or more variables into arrays, and of course includes array variables. The parameter is the name of the variable rather than the full name with $.
    The opposite function is extract(). As the name suggests, it converts the array into a single string, with the key value as its string name and the array value as the string value.
   Running result:

Copy code The code is as follows:


Array ( [number] => 1,3,5,7 ,9 [string] => I'm PHPer [array] => Array ( [0] => And [1] => You? ) )


2 .array_combine()——Reorganize two arrays into one array, one as the key value and the other as the value:
  array array_combine (array $keys, array $values)

Copy code The code is as follows:


$number = array("1","3","5","7","9");
$array = array("I","Am","A","PHP","er");
$newArray = array_combine($number,$array);
print_r ($ newArray);
?>


I won’t go into detail about the array_combine function, everyone will understand it
=> I [3] => Am [5] => A [7] => PHP [9] => er )
  3.range() function - Create an array within a specified range:
                        
#Copy code

array1 = range(0,100,10);//0 is the starting value, 100 is the end value, and 10 is the step value (the default step value is 1).

print_r($array1);

echo"
"; $array2 = range("A","Z"); print_r($array2);

echo "
";

$array3 = range("z","a");
print_r($array3);
?>



The default step value of the range() function it's 1!
Running results:



Copy code

The code is as follows:


Array ( [0] => 0 [1] => 10 [2] => 20 [3] => 30 [4] => 40 [5] => 50 [6] => 60 [7] => 70 [8] => 80 [9] => 90 [10] => 100 )
Array ( [0] => A [1] => B [2] => C [3] => D [4] => E [5] => F [6] => G [7] => H [8] => I [9] => J [10] => K [11] => L [12] => M [13] => N [14] => O [15] => P [16] => Q [17] => R [18] => S [19] => T [20] => U [21] => V [22] => W [23] => X [24] => Y [25] => Z )
Array ( [0] => z [1] => y [2] => x [3] => w [4] => v [5] => u [6] => t [7] => s [8] => r [9] => q [10] => p [11] => o [12] => n [13] => m [14] => l [15] => k [16] => j [17] => i [18] => h [19] => g [20] => f [21] => e [22] => d [23] => c [24] => b [25] => a )


4.array_fill()函数——填充数组函数:

复制代码 代码如下:


$array = range(1,10);
$fillarray = range("a","d");
$arrayFilled = array_fill(0,5,$fillarray);//这里的$fillarray可以是字符串,如"test".
echo "

"; <br>print_r ($arrayFilled); <br>echo "
";
$keys = array("string","2",9,"SDK","PK");
$array2 = array_fill_keys($keys,"testing");
echo "
"; <br>print_r ($array2); <br>echo "
";
?>


运行结果:

复制代码 代码如下:


Array
(
[0] => Array
(
[0] => a
[1] => b
[2] => c
[3] => d
)
[1] => Array
(
[0] => a
[1] => b
[2] => c
[3] => d
)
[2] => Array
(
[0] => a
[1] => b
[2] => c
[3] => d
)
[3] => Array
(
[0] => a
[1] => b
[2] => c
[3] => d
)
[4] => Array
(
[0] => a
[1] => b
[2] => c
[3] => d
)
)
Array
(
[string] => testing
[2] => testing
[9] => testing
[SDK] => testing
[PK] => testing
)


二、数组的遍历:
    1.foreach遍历:
       foreach (array_expression as $value){}
       foreach (array_expression as $key => $value){}
      闲话少说,上实例:

复制代码 代码如下:


$speed = array(50,120,180,240,380);
foreach($speed as $keys=>$values){
echo $keys."=>".$values."
";
}
?>


运行结果:

复制代码 代码如下:


0=>50
1=>120
2=>180
3=>240
4=>380


2.while循环遍历:
      while循环遍历一般结合list函数,以下是实例

复制代码 代码如下:


$staff = array(
array("姓名","性别","年龄"),
array("小张","男",24),
array("小王","女",25),
array("小李","男",23)
);
echo "

";
while(list($keys,$value) = each($staff)){
list($name,$sex,$age) = $value;
echo "";
}
echo "
$name$sex$age
";
?>


运行结果:

姓名 性别 年龄
小张 24
小王 25
小李 23

3.for loop traversal:

Copy code The code is as follows:


$speed = range( 0,220,20);
for($i =0;$iecho $speed[$i]." ";
}
? >


Run result:

Copy code The code is as follows:


0 20 40 60 80 100 120 140 160 180 200 220

The above is the detailed content of PHP array related explanations. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Clair Obscur: Expedition 33 - How To Get Perfect Chroma Catalysts
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1677
14
PHP Tutorial
1278
29
C# Tutorial
1257
24
PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

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's Purpose: Building Dynamic Websites PHP's Purpose: Building Dynamic Websites Apr 15, 2025 am 12:18 AM

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

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 and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

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.

Why Use PHP? Advantages and Benefits Explained Why Use PHP? Advantages and Benefits Explained Apr 16, 2025 am 12:16 AM

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

PHP's Impact: Web Development and Beyond PHP's Impact: Web Development and Beyond Apr 18, 2025 am 12:10 AM

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

PHP vs. Python: Use Cases and Applications PHP vs. Python: Use Cases and Applications Apr 17, 2025 am 12:23 AM

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.

The Continued Use of PHP: Reasons for Its Endurance The Continued Use of PHP: Reasons for Its Endurance Apr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

See all articles