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 <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">function get_students() {
$students[] = array("name" => "张三", "age" => 22);
$students[] = array("name" => "李四", "age" => 23);
$students[] = 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
// )
// )</pre><div class="contentsignin">Copy after login</div></div>
via the
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!