How to return all arrays in php

PHPz
Release: 2023-04-20 14:54:19
Original
482 people have browsed it

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
//         )
// )
Copy after login

Method two: Specify using the "relative array method" Array index and then return <div class="code" style="position:relative; padding:0px; margin:0px;"><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><div class="contentsignin">Copy after login</div></div> 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!

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