"张三", "age " => 22), array("name"/> "张三", "age " => 22), array("name">

Home >Backend Development >PHP Problem >How to return all arrays in php

How to return all arrays in php

PHPz
PHPzOriginal
2023-04-20 13:49:53517browse

In PHP, if you want to return the entire array, you can use the keyword return to return the entire array. The following are two methods of returning an array:

Method one: Return the entire array by directly using the return keyword

function get_students() {
    $students = array(
      array("name" => "张三", "age" => 22),
      array("name" => "李四", "age" => 23),
      array("name" => "王五", "age" => 24)
    );
    return $students;
}

$all_students = get_students();
print_r($all_students);

// 输出:
// Array
// (
//     [0] => Array
//         (
//             [name] => 张三
//             [age] => 22
//         )

//     [1] => Array
//         (
//             [name] => 李四
//             [age] => 23
//         )

//     [2] => Array
//         (
//             [name] => 王五
//             [age] => 24
//         )
// )

Method two: Specify using the "relative array method" Array index and then return <pre class="brush:php;toolbar:false">function get_students() {     $students[] = array(&quot;name&quot; =&gt; &quot;张三&quot;, &quot;age&quot; =&gt; 22);     $students[] = array(&quot;name&quot; =&gt; &quot;李四&quot;, &quot;age&quot; =&gt; 23);     $students[] = array(&quot;name&quot; =&gt; &quot;王五&quot;, &quot;age&quot; =&gt; 24);     return $students; } $all_students = get_students(); print_r($all_students); // 输出: // Array // ( //     [0] =&gt; Array //         ( //             [name] =&gt; 张三 //             [age] =&gt; 22 //         ) //     [1] =&gt; Array //         ( //             [name] =&gt; 李四 //             [age] =&gt; 23 //         ) //     [2] =&gt; Array //         ( //             [name] =&gt; 王五 //             [age] =&gt; 24 //         ) // )</pre> via the

return

keyword. In both methods, use return to return the entire array. The returned array will be stored in a variable, and then you can use print_r() or var_dump() to view the contents of the returned array.

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