PHP,从多维数组中的多个关联数组中回显一个特定值
PHP, echo one specific value from multiple associative arrays in a multidimensional array
我只想输出每个数组中的名字。
我在下面的代码中的评论解释。我不要钥匙,
只是价值。正如您将从我下面的描述中看到的那样,
我很想有一个可以产生预期结果的循环。
是否有一个循环只会 return 'firstName' 值?
谢谢....
<?php
$contacts = array(
array(
'firstName' => "Hoth",
'lastName' => "Omiaenoed",
'address'=> "76 S. Mammoth St.",
'city' => "Westfield",
'state' => "MA",
'zip' => "01085",
),
array(
'firstName' => "Akae",
'lastName' => "Uniaebul",
'address'=> "8038 Shadow Brook Street",
'city' => "Ocoee",
'state' => "FL",
'zip' => "34761",
),
array(
'firstName' => "Upae",
'lastName' => "Aesuudoes",
'address'=> "7044 N. School St.",
'city' => "Riverdale",
'state' => "GA",
'zip' => "30274",
),
array(
'firstName' => "Doer",
'lastName' => "Waezoeyo",
'address'=> "338 Old Griffin St.",
'city' => "Antioch",
'state' => "TN",
'zip' => "37013",
),
array(
'firstName' => "Ozae",
'lastName' => "Iraeoewif",
'address'=> "65 West Chapel Dr.",
'city' => "San Angelo",
'state' => "TX",
'zip' => "76901",
),
array(
'firstName' => "Naes",
'lastName' => "Ebaeaeraesh",
'address'=> "34 Tower Street",
'city' => "Huntersville",
'state' => "NC",
'zip' => "28078",
)
);
/* I want to echo (print) just the first names from each array. I hoped I could use a "foreach" loop to do this and it seemed to me the one just below should work */
foreach ($contacts['firstName'] as $fname) {
while (list( , $firstn) = each($fname)) {
echo "$firstn <br/>";
}
echo "------------------------------------------<br/>";
}
/* But it does not. Adding in ['firstName'] does not work. So, I have to use this one below, for lack of knowing what else to do. */
echo $contacts[0]['firstName']; echo "<br/>";
echo "------------------------------------------<br/>";
echo $contacts[1]['firstName']; echo "<br/>";
echo "------------------------------------------<br/>";
echo $contacts[2]['firstName']; echo "<br/>";
echo "------------------------------------------<br/>";
echo $contacts[3]['firstName']; echo "<br/>";
echo "------------------------------------------<br/>";
echo $contacts[4]['firstName']; echo "<br/>";
echo "------------------------------------------<br/>";
echo $contacts[5]['firstName']; echo "<br/>";
echo "------------------------------------------<br/>";
/* I used this to output each complete array and it works just fine. */
foreach ($contacts as $info) {
while (list( , $lines) = each ($info)) {
echo "$lines <br/>";
}
echo "<hr/>";
}
?>
<?php foreach ($contacts as $firtname) {
echo $firtname['firstName'] ;
}
?>
我想这就是你要找的..
如果使用 php 5.5+
则可以使用 array columns
$list_fn = array_column($contacts, 'firstName');
foreach ($contacts as $contact)
{
echo $contact['firstName'];
}
以下是您可以执行的操作。
$first_names = array_column($contacts, 'firstName'); // This returns what you need
foreach( $first_names as $name ){
echo $name . "<br/>";
echo "------------------------------------------<br/>";
}
希望对您有所帮助!
foreach($contacts as $contact){
echo $contact['firstName']."</br>";
}
我只想输出每个数组中的名字。 我在下面的代码中的评论解释。我不要钥匙, 只是价值。正如您将从我下面的描述中看到的那样, 我很想有一个可以产生预期结果的循环。 是否有一个循环只会 return 'firstName' 值? 谢谢....
<?php
$contacts = array(
array(
'firstName' => "Hoth",
'lastName' => "Omiaenoed",
'address'=> "76 S. Mammoth St.",
'city' => "Westfield",
'state' => "MA",
'zip' => "01085",
),
array(
'firstName' => "Akae",
'lastName' => "Uniaebul",
'address'=> "8038 Shadow Brook Street",
'city' => "Ocoee",
'state' => "FL",
'zip' => "34761",
),
array(
'firstName' => "Upae",
'lastName' => "Aesuudoes",
'address'=> "7044 N. School St.",
'city' => "Riverdale",
'state' => "GA",
'zip' => "30274",
),
array(
'firstName' => "Doer",
'lastName' => "Waezoeyo",
'address'=> "338 Old Griffin St.",
'city' => "Antioch",
'state' => "TN",
'zip' => "37013",
),
array(
'firstName' => "Ozae",
'lastName' => "Iraeoewif",
'address'=> "65 West Chapel Dr.",
'city' => "San Angelo",
'state' => "TX",
'zip' => "76901",
),
array(
'firstName' => "Naes",
'lastName' => "Ebaeaeraesh",
'address'=> "34 Tower Street",
'city' => "Huntersville",
'state' => "NC",
'zip' => "28078",
)
);
/* I want to echo (print) just the first names from each array. I hoped I could use a "foreach" loop to do this and it seemed to me the one just below should work */
foreach ($contacts['firstName'] as $fname) {
while (list( , $firstn) = each($fname)) {
echo "$firstn <br/>";
}
echo "------------------------------------------<br/>";
}
/* But it does not. Adding in ['firstName'] does not work. So, I have to use this one below, for lack of knowing what else to do. */
echo $contacts[0]['firstName']; echo "<br/>";
echo "------------------------------------------<br/>";
echo $contacts[1]['firstName']; echo "<br/>";
echo "------------------------------------------<br/>";
echo $contacts[2]['firstName']; echo "<br/>";
echo "------------------------------------------<br/>";
echo $contacts[3]['firstName']; echo "<br/>";
echo "------------------------------------------<br/>";
echo $contacts[4]['firstName']; echo "<br/>";
echo "------------------------------------------<br/>";
echo $contacts[5]['firstName']; echo "<br/>";
echo "------------------------------------------<br/>";
/* I used this to output each complete array and it works just fine. */
foreach ($contacts as $info) {
while (list( , $lines) = each ($info)) {
echo "$lines <br/>";
}
echo "<hr/>";
}
?>
<?php foreach ($contacts as $firtname) {
echo $firtname['firstName'] ;
}
?>
我想这就是你要找的..
如果使用 php 5.5+
则可以使用 array columns$list_fn = array_column($contacts, 'firstName');
foreach ($contacts as $contact)
{
echo $contact['firstName'];
}
以下是您可以执行的操作。
$first_names = array_column($contacts, 'firstName'); // This returns what you need
foreach( $first_names as $name ){
echo $name . "<br/>";
echo "------------------------------------------<br/>";
}
希望对您有所帮助!
foreach($contacts as $contact){
echo $contact['firstName']."</br>";
}