本文将介绍如何在 PHP 中显示数组值。foreach 循环可用于显示每个值。print_r() 或 var_dump 等内置函数可以转储数组的所有信息。我们还可以将数组转换为字符串以显示值。在 php 中
foreach
循环可用于显示每个值。print_r()
或 var_dump
等内置函数可以转储数组的所有信息。
我们还可以将数组转换为字符串以显示值。
foreach
循环显示数组值foreach
循环可以回显数组的每个值。由于关联数组同时具有键和值,因此我们将两者都显示。
<?php
//Simple one dimensional array
$demo_array1 = array('Jack', 'Shawn', 'Michelle', 'Maria');
//associative array
$demo_array2 = array(1 => 'Jack', 2 => 'Shawn', 3 => 'Michelle', 4 => 'Maria');
echo "The values for first array are: <br>";
foreach($demo_array1 as $value){
echo $value."<br>";
}
echo "The values for associative array: <br>";
foreach($demo_array2 as $key => $value){
echo "The Key <b>". $key."</b> has the value <b>".$value."</b><br>" ;
}
?>
输出:
The values for first array are:
Jack
Shawn
Michelle
Maria
The values for associative array:
The Key **1** has the value **Jack**
The Key **2** has the value **Shawn**
The Key **3** has the value **Michelle**
The Key **4** has the value **Maria**
print_r()
和 var_dump()
显示数组的信息内置函数 print_r()
和 var_dump()
用于转储 PHP 中的数组信息。
<?php
//Simple one dimensional array
$demo_array1 = array('Jack', 'Shawn', 'Michelle', 'Maria');
//associative array
$demo_array2 = array('Name1' => 'Jack', 'Name2' => 'Shawn', 'Name3' => 'Michelle', 'Name4' => 'Maria');
echo "The values for first array using print_r: <br>";
print_r($demo_array1);
echo "<br>The values for first array using var_dump: <br>";
var_dump($demo_array1);
echo "<br>The values for associative array using print_r: <br>";
print_r($demo_array2);
echo "<br>The values for associative array using var_dump: <br>";
var_dump($demo_array2);
?>
输出:
The values for first array using print_r:
Array ( [0] => Jack [1] => Shawn [2] => Michelle [3] => Maria )
The values for first array using var_dump:
array(4) { [0]=> string(4) "Jack" [1]=> string(5) "Shawn" [2]=> string(8) "Michelle" [3]=> string(5) "Maria" }
The values for associative array using print_r:
Array ( [Name1] => Jack [Name2] => Shawn [Name3] => Michelle [Name4] => Maria )
The values for associative array using var_dump:
array(4) { ["Name1"]=> string(4) "Jack" ["Name2"]=> string(5) "Shawn" ["Name3"]=> string(8) "Michelle" ["Name4"]=> string(5) "Maria" }
print_r
将只显示数组的键值。但是,var_dump
将显示数组长度、值长度、键和值。
implode()
或 JSON_encode()
将数组转换为字符串内置函数 implode()
或 json_encode()
可用于将数组转换为字符串以显示值。
<?php
//Simple one dimensional array
$demo_array1 = array('Jack', 'Shawn', 'Michelle', 'Maria');
//associative array
$demo_array2 = array('Name1' => 'Jack', 'Name2' => 'Shawn', 'Name3' => 'Michelle', 'Name4' => 'Maria');
echo "The values for first array using implode: <br>";
$implode_string=implode(",",$demo_array1);
echo $implode_string."<br>";
$implode_string1=implode(",",$demo_array2);
echo $implode_string1."<br>";
echo "The values for associative array using json_encode: <br>";
$json_string=json_encode($demo_array1);
echo $json_string."<br>";
$json_string1=json_encode($demo_array2);
echo $json_string1."<br>";
?>
输出:
The values for the first array using implode:
Jack,Shawn,Michelle,Maria
Jack,Shawn,Michelle,Maria
The values for associative array using json_encode:
["Jack","Shawn","Michelle","Maria"]
{"Name1":"Jack","Name2":"Shawn","Name3":"Michelle","Name4":"Maria"}
正如我们所见,implode()
不显示关联数组的键,但 json_encode()
也显示关联数组的键。
foreach
循环和 array_map()
函数显示多维数组的值我们将使用 foreach
循环或 array_map
内置函数将值显示为二维数组的列表。
<?php
$demo_array = Array (
0 => Array ( "Name" => "Jack" , "Salary" => 4000 ) ,
1 => Array ( "Name" => "Mike" , "Salary" => 3500 ) ,
2 => Array ( "Name" => "Tina" , "Salary" => 3000 ) );
//Using foreach loop
echo "<pre>";
echo "Name\tSalary";
foreach ( $demo_array as $value ) {
echo "\n", $value['Name'], "\t", $value['Salary'];
}
//Using array_map() function
echo "<pre>" ;
echo "Name\tSalary";
array_map(function ($value) {
echo "\n", $value['Name'], "\t", $value['Salary'];
}, $demo_array);
?>
上面的代码将显示带有薪水的姓名列表。
输出:
Name Salary
Jack 4000
Mike 3500
Tina 3000
Name Salary
Jack 4000
Mike 3500
Tina 3000
同样,我们可以为具有更多层的多维数组设置多个 foreach
循环,或者在 array_map()
参数中设置一个函数。
--结束END--
本文标题: PHP 显示数组值
本文链接: https://lsjlt.com/news/568994.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0