Home  >  Article  >  Backend Development  >  How to use values ​​in an array in php

How to use values ​​in an array in php

WBOY
WBOYOriginal
2023-05-07 17:31:07468browse

[Preface]

Array is a very commonly used data type when programming in PHP. Multiple elements can be stored in an array, and each element can be of any data type. In this article, we will explore how to perform data operations using values ​​in arrays.

[Main text]

To use the values ​​in the array, we first need to understand how to access the elements in the array. In PHP, there are two ways to access elements of an array, namely through subscripting and through loops.

  1. Use subscripts to access array elements

In PHP, the most basic way to access array elements is through subscripts. The subscript can be any number or string, which corresponds to the elements in the array. For example:

$fruits = array("apple", "banana", "orange");
echo $fruits[0]; // 输出 "apple"
echo $fruits[1]; // 输出 "banana"
echo $fruits[2]; // 输出 "orange"

In addition, you can also use associative arrays to access array elements using string subscripts, for example:

$person = array("name" => "张三", "age" => 25, "gender" => "男");
echo $person["name"]; // 输出 "张三"
echo $person["age"]; // 输出 "25"
echo $person["gender"]; // 输出 "男"
  1. Use loops to access array elements

If you need to traverse all the elements in an array, you can use a loop to access. In PHP, there are three loop methods that can be used to traverse arrays, namely foreach, for and while.

  • Use foreach to loop through the array
$fruits = array("apple", "banana", "orange");
foreach($fruits as $fruit) {
    echo $fruit . " ";
}
// 输出:"apple banana orange"
$person = array("name" => "张三", "age" => 25, "gender" => "男");
foreach($person as $key => $value) {
    echo $key . ":" . $value . "<br>";
}
// 输出:"name:张三 age:25 gender:男"
  • Use for loop to traverse the array
$fruits = array("apple", "banana", "orange");
for($i = 0; $i < count($fruits); $i++) {
    echo $fruits[$i] . " ";
}
// 输出:"apple banana orange"
$person = array("name" => "张三", "age" => 25, "gender" => "男");
$keys = array_keys($person);
for($i = 0; $i < count($keys); $i++) {
    $key = $keys[$i];
    $value = $person[$key];
    echo $key . ":" . $value . "<br>";
}
// 输出:"name:张三 age:25 gender:男"
  • Use while loop to traverse the array
$fruits = array("apple", "banana", "orange");
$i = 0;
while($i < count($fruits)) {
    echo $fruits[$i] . " ";
    $i++;
}
// 输出:"apple banana orange"
$person = array("name" => "张三", "age" => 25, "gender" => "男");
$keys = array_keys($person);
$i = 0;
while($i < count($keys)) {
    $key = $keys[$i];
    $value = $person[$key];
    echo $key . ":" . $value . "<br>";
    $i++;
}
// 输出:"name:张三 age:25 gender:男"

No matter which loop method is used, how to use the values ​​in the array is the same. Once we have the values ​​in the array, we can use it to perform various data operations.

  1. Use the values ​​in the array to perform data operations

In PHP, the values ​​in the array can be used to perform various data operations, such as string splicing and mathematical calculations etc.

  • String concatenation
$firstName = "张";
$lastName = "三";
$name = $firstName . $lastName;
echo $name; // 输出:"张三"
$person = array("name" => "张三", "age" => 25, "gender" => "男");
$name = $person["name"];
$age = $person["age"];
$gender = $person["gender"];
echo "姓名:" . $name . " 年龄:" . $age . " 性别:" . $gender;
// 输出:"姓名:张三 年龄:25 性别:男"
  • Mathematical calculation
$width = 5;
$height = 10;
$area = $width * $height;
echo "长方形的面积为:" . $area;
// 输出:"长方形的面积为:50"
$person = array("name" => "张三", "age" => 25, "gender" => "男");
$age = $person["age"];
if($age >= 18) {
    echo "成年人";
} else {
    echo "未成年人";
}
// 输出:"成年人"

[Conclusion]

Arrays are PHP programming It is a very commonly used data type in , and it is also very convenient to use the values ​​in the array to perform data operations. In this article, we learned how to access the elements in an array and how to use the values ​​in the array to perform operations such as string concatenation and mathematical calculations. Hope this article is helpful to PHP developers.

The above is the detailed content of How to use values ​​in an array in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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