现在我有一个检索数据表单数组的问题,也许它真的很简单(可能是这样)但是我从早上起就一直在努力,看来,我对PHP的了解是没有价值的...所以我有几个阵列:
array( "name" => "Array 1",
"type" => "array"),
array( "name" => "Array 2",
"type" => "whatever"),
array( "name" => "Array 3",
"type" => "whatever"),
array( "name" => "Array 4",
"type" => "array"),
array( "name" => "Array 5",
"type" => "whatever"),
我需要做的是显示'array'类型数组的'name',我知道我需要一个foreach循环,但是如何构造'foreach($ arrays as $ array){'来获得所需的结果?
修改
感谢所有的回复,但我知道我没有说清楚。我只有在有“type”=>时才需要显示“name”数组中存在“array”,其他每个数组的“name”都应该省略。
答案 0 :(得分:1)
你需要做这样的事情:
foreach($arrays as $array) {
if($array['type'] == 'array') {
print($array['name']);
}
}
答案 1 :(得分:1)
array_map(function ($v){ if($v['type']=="array"){echo $v['name']."<br>";}},$arr);
<强> OUTPUT :
强>
Array 1
Array 4
答案 2 :(得分:0)
将数组放入&#34;容器&#34;数组首先,然后你可以使用那个......
<?php
$data = array(
array( "name" => "Array 1",
"type" => "array"),
array( "name" => "Array 2",
"type" => "whatever"),
array( "name" => "Array 3",
"type" => "whatever"),
array( "name" => "Array 4",
"type" => "array"),
array( "name" => "Array 5",
"type" => "whatever")
);
foreach($data as $array) {
if($array['type'] == 'array') {
print($array['name']);
}
}