欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

索引多维数组_PHP数组–索引,关联和多维

程序员文章站 2024-03-15 12:32:17
...

索引多维数组

An array is a group of similar types of variables. PHP provides function array() to create a PHP array.

数组是一组相似类型的变量。 PHP提供函数array()创建一个PHP数组。

PHP数组 (PHP Array)

索引多维数组_PHP数组–索引,关联和多维

There are three types of arrays in PHP.


PHP中存在三种类型的数组。

  1. Indexed arrays – Array with numeric indexes.

    索引数组 –具有数字索引的数组。
  2. Associative arrays – Array with key-value pairs, its similar to Map in java.

    关联数组 –具有键值对的数组,类似于Java中的Map。
  3. Multidimensional arrays – An array of arrays.

    多维数组 –数组数组。

PHP索引数组 (PHP Indexed arrays)

There are two ways to create indexed arrays.
– first way to use array() function without any index, index are assigned automatically starting from 0.
– second way to manually assign index and create the array.

有两种创建索引数组的方法。
–不使用任何索引使用array()函数的第一种方法,索引从0开始自动分配。
–手动分配索引并创建数组的第二种方法。

PHP count() function is used to get the length of an array. We can use for loop to loop through all the values of an indexed array.

PHP count()函数用于获取数组的长度。 我们可以使用for循环遍历索引数组的所有值。

Below code shows both the ways to create an indexed array and loop through them in PHP.

下面的代码显示了两种创建索引数组并在PHP中循环遍历的方法。

<?php
$colors = array("Red","Green","Blue");

$colors1[0] = "Red";
$colors1[1] = "Green";

$length = count($colors);
echo "colors array length=" . $length; // prints "colors array length=3"
echo "<br>";
echo "colors1 array length=" . count($colors1); // prints "colors1 array length=2"

//looping an indexed array
for($i=0; $i<$length; $i++){
	echo $colors[$i];
	echo "<br>";
}
?>

PHP关联数组 (PHP Associative Arrays)

Associative arrays uses named keys for values and we can create them in similar way like indexed arrays. foreach is used to loop through an associative array.

关联数组对值使用命名键,我们可以像建立索引数组一样创建它们。 foreach用于遍历关联数组。

<?php

$colors = array("0"=>"Red","1"=>"Green","2"=>"Blue");

echo "0th element of array is " . $colors["0"];
echo "<br>";
//looping
foreach ($colors as $key=>$value){
	echo "Key=".$key." value=".$value;
	echo "<br>";
}
?>

output of above PHP script is:

上面PHP脚本的输出是:

0th element of array is Red
Key=0 value=Red
Key=1 value=Green
Key=2 value=Blue

PHP中的多维数组 (Multidimensional Array in PHP)

A multidimensional array is an array of arrays. We can create two-dimensional, three-dimensional and n-dimensional arrays using array function.

多维数组是数组的数组。 我们可以使用数组函数创建二维,三维和n维数组。

Each array within the multidimensional array can be either indexed array or associative array. We can use for loop for looping through indexed array and foreach for looping through associative array.

多维数组中的每个数组都可以是索引数组或关联数组。 我们可以使用for循环遍历索引数组,使用foreach遍历关联数组。

In below example, I am showing two-dimensional indexed arrays and mixture of indexed and associative array in multidimensional array.

在下面的示例中,我显示了二维索引数组以及多维数组中索引数组和关联数组的混合。

We can use print_r() or var_dump() function to print the human readable form of the array.

我们可以使用print_r()var_dump()函数来打印人类可读的数组形式。

<?php

//Indexed two-dimensional array
$cars = array(
				array("Honda Accord", "V6", 30000),
				array("Toyota Camry", "LE", 24000),
				array("Nissan Altima", "V1"),
			);

//printing array information			
print_r($cars);
echo "<br>";
var_dump($cars);
echo "<br>";
//looping through two-dimensional indexed array

for($i=0;$i<count($cars);$i++){
	for($j=0;$j<count($cars[$i]);$j++){
		echo $cars[$i][$j] . " ";
	}
	echo "<br>";
}
echo "<br><br>";

//Indexed Associative two-dimensional array
$cars = array(
				array("Name" => "Honda Accord", "Model" => "V6", "Cost" => 30000),
				array("Name" => "Toyota Camry", "Model" => "LE", "Cost" => 24000),
				array("Name" => "Nissan Altima", "Model" => "V1"),
			);

//printing array information			
print_r($cars);
echo "<br>";
var_dump($cars);
echo "<br>";

//looping through two-dimensional indexed associative array

for($i=0;$i<count($cars);$i++){
	$c=0;
	foreach($cars[$i] as $key=>$value){
		$c++;
		echo $key."=".$value;
		if($c<count($cars[$i])) echo ",";
	}
	echo "<br>";
}
?>

The output of above PHP script is:

上面PHP脚本的输出为:

Array ( [0] => Array ( [0] => Honda Accord [1] => V6 [2] => 30000 ) [1] => Array ( [0] => Toyota Camry [1] => LE [2] => 24000 ) [2] => Array ( [0] => Nissan Altima [1] => V1 ) ) 
array(3) { [0]=> array(3) { [0]=> string(12) "Honda Accord" [1]=> string(2) "V6" [2]=> int(30000) } [1]=> array(3) { [0]=> string(12) "Toyota Camry" [1]=> string(2) "LE" [2]=> int(24000) } [2]=> array(2) { [0]=> string(13) "Nissan Altima" [1]=> string(2) "V1" } } 
Honda Accord V6 30000 
Toyota Camry LE 24000 
Nissan Altima V1 


Array ( [0] => Array ( [Name] => Honda Accord [Model] => V6 [Cost] => 30000 ) [1] => Array ( [Name] => Toyota Camry [Model] => LE [Cost] => 24000 ) [2] => Array ( [Name] => Nissan Altima [Model] => V1 ) ) 
array(3) { [0]=> array(3) { ["Name"]=> string(12) "Honda Accord" ["Model"]=> string(2) "V6" ["Cost"]=> int(30000) } [1]=> array(3) { ["Name"]=> string(12) "Toyota Camry" ["Model"]=> string(2) "LE" ["Cost"]=> int(24000) } [2]=> array(2) { ["Name"]=> string(13) "Nissan Altima" ["Model"]=> string(2) "V1" } } 
Name=Honda Accord,Model=V6,Cost=30000
Name=Toyota Camry,Model=LE,Cost=24000
Name=Nissan Altima,Model=V1

In next posts, we will look into some common array functions.

在下一篇文章中,我们将研究一些常见的数组函数。

Earlier articles for PHP tutorial:

早期PHP教程文章:

  1. PHP Tutorial for Beginners – PHP Syntax, Comments, Variables and Scope

    PHP初学者教程– PHP语法,注释,变量和范围
  2. PHP Operators – Arithmetic, Comparison, Logical, Bitwise Operators

    PHP运算符–算术,比较,逻辑,按位运算符
  3. PHP if-else, Switch Case and Shorthand Ternary operator

    PHP if-else,切换大小写和缩写三元运算符

翻译自: https://www.journaldev.com/1528/php-array-indexed-associative-multidimensional

索引多维数组