Four methods of php output array

Posted by afern03 on Sun, 02 Jan 2022 20:03:35 +0100

In previous articles< PHP array learning how to create and initialize a one-dimensional array (detailed code) >And< Analysis of two-dimensional array creation method for PHP array learning >In, we introduce the definition method of one-dimensional array and two-dimensional array through code examples, which is simple and easy to understand.

Now that the array is created and initialized, after some operation, you need to output and print the operation results. So how do PHP arrays output? The following article will introduce several methods of array output: print_r(),var_dump(),echo/print. (attached: Video Explanation of array function of PHP function)

1. Using print_r() output array

print_ The R () function is used to print variables in a more understandable form through print_ The R () function can output the contents and structure of the entire array, and display keys and elements in a certain format.

<?php
header("Content-type:text/html;charset=utf-8");
$array = array
(
    array("full name"=>"Zhang San","Age"=>25,"Gender"=>"male"),
    array("full name"=>"Li Si","Age"=>21,"Gender"=>"male"),
    array("full name"=>"Nana","Age"=>22,"Gender"=>"female")
);
 print_r($array);
?>

Output results:

Array ( [0] => Array ( [full name] => Zhang San [Age] => 25 [Gender] => male ) [1] => Array ( [full name] => Li Si [Age] => 21 [Gender] => male ) [2] => Array ( [full name] => Nana [Age] => 22 [Gender] => female ) )

Does it feel that the output result is too long and does not use reading. You can print_ Add a code before the R () output statement:

<?php
header("Content-type:text/html;charset=utf-8");
$array = array
(
    array("full name"=>"Zhang San","Age"=>25,"Gender"=>"male"),
    array("full name"=>"Li Si","Age"=>21,"Gender"=>"male"),
    array("full name"=>"Nana","Age"=>22,"Gender"=>"female")
);
echo '<pre>';
 print_r($array);
?>

In this way, the output results are more conducive to reading. The output results are:

2. Use var_dump() output array

Same as print_ The R () function is similar to var_ The dump () function can also output the data content and structure of the entire array. But var_dump() is better than print_r() is more powerful. It can print multiple variables at the same time and give the type information of variables.

var_ The dump () function can output the relevant information (type and value) of variables. When outputting an array, the array will expand the value recursively and display its structure through indentation.

<?php
header("Content-type:text/html;charset=utf-8");
$array = array
(
    array("full name"=>"Zhang San","Age"=>25,"Gender"=>"male"),
    array("full name"=>"Li Si","Age"=>21,"Gender"=>"male"),
    array("full name"=>"Nana","Age"=>22,"Gender"=>"female")
);
 var_dump($array);
?>

Output results:

explain:

print_r() and VaR_ The dump () function is generally used by debuggers. More often, echo is used to output specific array cell values.

3. Use echo/print to output an array

echo() is used to output one or more strings.

Strictly speaking, echo is not a function, it is actually a language structure; Therefore, it is not necessary to use parentheses to indicate parameters. Single quotation marks and double quotation marks can also be used.

However, if you pass more than one parameter to echo(), you cannot use parentheses because a parsing error will be generated.

print() is a bit similar to echo() (it is actually a language structure), but the difference is that echo can accept multiple parameters and has no return value, while print() can only accept one parameter and has a return value.

When using echo/print for array output, only one element in an array can be output. Let's take a concrete look at the following code examples:

<?php
header("Content-type:text/html;charset=utf-8");
$array= array("Banana","Apple","Pear","orange","a mandarin orange","Durian");
//Output statement
echo '$array[0] = '.$array[0].'<br>';
echo '$array[1] = '.$array[1].'<br>';
echo '$array[2] = '.$array[2].'<br>';

print '$array[3] = '.$array[3].'<br>';
print '$array[4] = '.$array[4].'<br>';
print '$array[5] = '.$array[5].'<br>';
?>

Output:

$array[0] = Banana
$array[1] = Apple
$array[2] = Pear
$array[3] = orange
$array[4] = a mandarin orange
$array[5] = Durian

" number group change amount name [ lower mark ] " shape type of language sentence yes use come interview ask number group in element element of , with Indeed set need want transport Out of element element . example as upper noodles of example son in , " The statement in the form of array variable name [subscript] "is used to access the elements in the array to determine the elements to be output. For example, in the above example“ The statement in the form of array variable name [subscript] "is used to access the elements in the array to determine the elements to be output. For example, in the above example, "array[0]" is to access the first element in the array (the index array starts from zero), so the "echo $array[0]" statement can output "banana".

If you want to output all the elements in the array, you need to traverse the array with the help of circular statements and output the elements in the array one by one.

Let's take the foreach statement as an example to see in detail how to cycle through the array and output all the elements in the array: (attached video tutorial: foreach loop )

<?php
header("Content-type:text/html;charset=utf-8");
$array= array("Banana","Apple","Pear","orange","a mandarin orange","Durian");
foreach ($array as $val){  // Traversal array
	//Output array elements
    echo $val."<br>";  
}

?>

Output results:

Banana
 Apple
 Pear
 orange
 a mandarin orange
 Durian

This article is reproduced from: https://www.php.cn/php-weizijiaocheng-480630.html

Recommended:< Summary of PHP interview questions in 2021 (Collection)><php video tutorial>

Topics: PHP array