Home > Backend Development > PHP Problem > How to return an array in php

How to return an array in php

PHPz
Release: 2023-04-19 10:06:14
Original
127 people have browsed it

PHP is a very popular programming language mainly used for developing web applications. In PHP, array is a very important data structure that can be used to store multiple values ​​and can be easily accessed through key-value pairs. In this article, we will explain how to return an array in PHP.

Arrays in PHP can be created in a variety of ways, the most commonly used of which is to use the array() function. The following is an example of using the array() function to create an array containing three elements:

<code>$my_array = array('apple', 'orange', 'banana');</code>
Copy after login

The above code defines an array named $my_array and adds the three strings 'apple', ' orange' and 'banana' are assigned to the 1st, 2nd and 3rd elements of the array respectively.

Sometimes, in programming, we need to use an array as the return value of a function so that subsequent code can further process the array. In PHP, there are two commonly used methods to return arrays.

Method 1: Use the return statement to return an array

In PHP, you can use the return statement to return an array as the return value of a function. The following is an example of using the return statement to return an array:

<code>function get_fruits() {
  $my_array = array('apple', 'orange', 'banana');
  return $my_array;
}

// 调用函数并输出返回值
$fruits = get_fruits();
print_r($fruits);</code>
Copy after login

The above code defines a function called get_fruits(), which creates an array of three elements and uses the return statement to Returned as the return value of the function. When we call this function, we can assign the returned array to the $fruits variable and use the print_r() function to output the array.

Output result:

<code>Array
(
    [0] => apple
    [1] => orange
    [2] => banana
)</code>
Copy after login
Copy after login

Method 2: Use reference transfer to return an array

In PHP, you can also return an array by reference transfer. When defining a function, you need to add the & symbol in front of the function parameter to indicate that the parameter is a reference parameter. In the function, by modifying this parameter, the external array can be modified. The following is an example of returning an array using reference passing:

<code>function get_fruits(&$result) {
  $my_array = array('apple', 'orange', 'banana');
  $result = $my_array;
}

// 调用函数并输出返回值
$fruits = array();
get_fruits($fruits);
print_r($fruits);</code>
Copy after login

The above code defines a function named get_fruits(), which creates an array $my_array containing three elements and passes the array The value is assigned to the result parameter by passing by reference. Outside the function, we define an empty array $fruits and pass it as an argument to the function. When the function is executed, the $fruits array is assigned the value of the $my_array array.

Output result:

<code>Array
(
    [0] => apple
    [1] => orange
    [2] => banana
)</code>
Copy after login
Copy after login

Summary

In PHP, there are two common ways to return an array: using the return statement to return an array and using reference passing to return an array. Both of the above methods can meet our different needs in programming. Learning how to return an array allows us to write PHP code more efficiently.

The above is the detailed content of How to return 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