我一直在玩array_search。我一直在尝试搜索数组并从中获取详细信息。问题是,一旦发现它继续运行的结果,它就不会检查数组中的重复项。我似乎无法解决我的循环逻辑,有什么建议吗?
$names
数组
Array(
[0] => Array
(
[name] => Fred
[age] => 20
)
[1] => Array
(
[name] => George
[age] => 17
)
[2] => Array
(
[name] => Sarah
[age] => 22
)
[3] => Array
(
[name] => Fred
[age] => 30
)
)
$name=" Fred";
//loop here
$array_position = array_search($name, array_column($names, 'name'));
$array_data = $names[$array_position];
echo $array_data['name'] . " is: " . $array_data['age'] . " years old.
//end loop
结果:
弗雷德已经20岁了。预期结果:
弗雷德已经20岁了 弗雷德今年30岁。我试过array_keys()
但我不太了解php.net
我应该尝试将结果存储在一个单独的数组中然后循环打印吗?似乎应该有一个更有效/更优雅的解决方案。
答案 0 :(得分:2)
使用array_filter查找全部。
function search($array, $key)
{
return array_filter($array, function($v) use($key){return $v['name'] == $key;});
}
$result = search($array, $key);
foreach($result as $v)
{
echo $v['name'] . " is: " . $v['age'] . " years old.";
}
答案 1 :(得分:1)
简单的foreach循环有什么问题?
// Declare the array
$names = array(
array(
'name' => 'Fred',
'age' => 20
),
array(
'name' => 'George',
'age' => 17
),
array(
'name' => 'Sarah',
'age' => 22
),
array(
'name' => 'Fred',
'age' => 30
)
);
// Name to search
$name = 'Fred';
foreach($names as $index => $data){
if($data['name'] == $name)
echo $data['name'] . ' is ' . $data['age'] . ' years old.<br>';
}
结果将是:
Fred is 20 years old.
Fred is 30 years old.
答案 2 :(得分:1)
array_keys()可以做到这一点:
<?php
$names = array(
array(
'name' => 'Fred',
'age' => 20
),
array(
'name' => 'George',
'age' => 17
),
array(
'name' => 'Sarah',
'age' => 22
),
array(
'name' => 'Fred',
'age' => 30
)
);
$matchKeys = array_keys( array_column($names, 'name'), 'Fred' );
array_map(function($arrKey) use ($names) {
echo $names[$arrKey]['name'] . " is: " . $names[$arrKey]['age'] . " years old.\n";
}, $matchKeys);
结果:
Fred is: 20 years old.
Fred is: 30 years old.
答案 3 :(得分:1)
@Ben首先是array_search()返回true或false而不是键,但是对于你想要的输出,你可以简单地尝试使用foreach(),条件如下或者使用array_key:
使用foreach()
<?php
$names = array(0 =>
array(
"name" => "Fred",
"age" => 20
),
1 => array(
"name" => "George",
"age" => 17
),
2 => array(
"name" => "Sarah",
"age" => 22
),
3 => array(
"name" => "Fred",
"age" => 30
)
);
$name= "Fred";
echo "<pre>";
foreach($names as $val){
if($val["name"] == $name){
echo $name . " is: " . $val["age"] . " years old.\n";
}
}
with array_key()
<?php
$names = array(0 =>
array(
"name" => "Fred",
"age" => 20
),
1 => array(
"name" => "George",
"age" => 17
),
2 => array(
"name" => "Sarah",
"age" => 22
),
3 => array(
"name" => "Fred",
"age" => 30
)
);
$name=" Fred";
$array_position = array_keys(array_column($names, "name"), "Fred");
foreach($array_position as $key){
echo $names[$key]["name"] . " is: " . $names[$key]["age"] . " years old.\n";
}
输出:
弗雷德是:20岁。 弗雷德是:30岁。答案 4 :(得分:0)
Array(
[0] => Array
(
[name] => Fred,
[age] => 20
),
[1] => Array
(
[name] => George,
[age] => 17
),
[2] => Array
(
[name] => Sarah,
[age] => 22
),
[3] => Array
(
[name] => Fred,
[age] => 30
)
)
$name="Fred";
//loop here
for($i =0; i<count($names); $i++){
if($arr[$i] == $name)echo $name . ' is ' . $names[$i] ."years old.<br>";
}
//end loop
输出:
Fred is 20 years old
Fred is 30 years old
答案 5 :(得分:0)
这可以提供帮助 -
$check_name= 'Fred'; // set the name to check
$temp= array_filter($names, function($a) use($check_name) {
return $a['name'] === $check_name; // return if matches
});
// loop through the result
foreach($temp as $v) {
echo $v['name'] . ' is ' . $v['age'] . ' years old.<br>';
}
<强>输出强>
Fred is 20 years old.
Fred is 30 years old.
答案 6 :(得分:0)
我不明白为什么你在$ name变量中给了空格 $ name =&#34;佛瑞德&#34 ;;
这是你的代码,
$name=" Fred";
function array_search_custom($needle,$haystack) {
$temp = [];
foreach($haystack as $key=>$value) {
if(trim($needle)===trim($value)) {
$temp[] =$key;
}
}
return $temp;
}
$name=" Fred";
$a_names = array_column($names, 'name');
$a_ages = array_column($names, 'age');
$array_position = array_search_custom($name, $a_names);
foreach($a_names as $k => $v){
if($array_position && in_array($k, $array_position)){
echo $a_names[$k] . " is: " . $a_ages[$k] . " years old.<br/>";
}
}
我修剪了你的名字并根据你的要求编写了自定义数组搜索功能。
尝试一下,它会起作用。