我只想输出每个数组的名字。 我在下面的代码中的注释解释。我不想要钥匙, 只是价值。正如您将从下面描述的内容中看到的, 我希望有一个能产生预期效果的循环。 是否存在仅返回'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/>";
}
?>
答案 0 :(得分:1)
<?php foreach ($contacts as $firtname) {
echo $firtname['firstName'] ;
}
?>
我认为这就是你要找的......
答案 1 :(得分:0)
答案 2 :(得分:0)
tool
答案 3 :(得分:0)
你可以做什么。
$first_names = array_column($contacts, 'firstName'); // This returns what you need
foreach( $first_names as $name ){
echo $name . "<br/>";
echo "------------------------------------------<br/>";
}
希望这有助于你!
答案 4 :(得分:0)
foreach($contacts as $contact){
echo $contact['firstName']."</br>";
}