Home > Backend Development > PHP Problem > How to get the value in an array in php

How to get the value in an array in php

PHPz
Release: 2023-04-24 09:16:21
Original
728 people have browsed it

In PHP, array is a very common data structure. Normally, we often need to get a value in an array in order to use or manipulate it. In this article, I will explain how to get values ​​from an array in PHP.

  1. Get the array value through subscript

Each element in the array has a unique subscript, and you can get a certain value in the array through the subscript . The subscript can be a number or a string, depending on the form used when the array is defined.

The following is some sample code that demonstrates how to get the value in the array through the subscript:

//创建一个数字下标数组
$numbers = array(1, 2, 3, 4, 5);

//通过下标获取第一个元素
$first = $numbers[0];
echo $first; //输出1

//创建一个字符串下标数组
$fruits = array("apple" => "red", "banana" => "yellow", "orange" => "orange");

//通过下标获取值
$color = $fruits["banana"];
echo $color; //输出yellow
Copy after login
  1. Use a foreach loop to get the array value

Use foreach Looping is a simple way to get all the values ​​in an array. This approach is particularly useful when working with associative arrays (i.e. with more readable string subscripts).

The following is a sample code that uses a foreach loop to obtain an array value:

//创建一个关联数组
$person = array(
    "name" => "John",
    "age" => 30,
    "email" => "john@example.com"
);

//循环遍历数组并输出所有值
foreach($person as $key => $value) {
    echo "$key : $value <br>";
}

//输出:
//name : John
//age : 30
//email : john@example.com
Copy after login
  1. Use the array_values() function to obtain an array value

If you just want to get the values ​​in the array and don't care about the key names, you can use PHP's array_values() function to extract all the values ​​from the array.

The following is a sample code to get an array value using the array_values() function:

//创建一个关联数组
$person = array(
    "name" => "John",
    "age" => 30,
    "email" => "john@example.com"
);

//使用 array_values() 函数获取值
$values = array_values($person);

//循环遍历数组并输出所有值
foreach($values as $value) {
    echo "$value <br>";
}

//输出:
//John
//30
//john@example.com
Copy after login

Summary

In PHP, there are many ways to get an array value in . No matter which method you use, you should understand their respective advantages and limitations so that you can use them when appropriate. I hope this article helped you better understand the use of arrays in PHP.

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

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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template