如果我有以下数组:
$Unmanaged = array(
array('name' => 'SD2005', 'speed' => '1000', 'size' => 'desk', 'uplink' => 'no'),
array('name' => 'SD2008', 'speed' => '1000', 'size' => 'desk', 'uplink' => 'no'),
array('name' => 'SR2016', 'speed' => '1000', 'size' => 'rack', 'uplink' => 'no'),
array('name' => 'SR2024', 'speed' => '1000', 'size' => 'rack', 'uplink' => 'no'),
array('name' => 'SR2024C', 'speed' => '1000', 'size' => 'rack', 'uplink' => 'no'),
array('name' => 'SD205', 'speed' => '100', 'size' => 'desk', 'uplink' => 'no'),
array('name' => 'SD208', 'speed' => '100', 'size' => 'desk', 'uplink' => 'no'),
array('name' => 'SD216', 'speed' => '100', 'size' => 'desk', 'uplink' => 'no'),
array('name' => 'SR224G', 'speed' => '100', 'size' => 'rack', 'uplink' => 'yes'),
array('name' => 'SR216', 'speed' => '100', 'size' => 'rack', 'uplink' => 'no'),
array('name' => 'SR224', 'speed' => '100', 'size' => 'rack', 'uplink' => 'no'),
array('name' => 'SR224R', 'speed' => '100', 'size' => 'rack', 'uplink' => 'no')
);
如何循环遍历所有条目,返回满足特定条件的所有条目,例如:速度为100的所有交换机:100,大小:桌面和上行链路:否?
请帮忙。
谢谢, 本
答案 0 :(得分:5)
以下是使用array_filter()
:
function my_callback($elem) {
return $elem['speed']==100 && $elem['size']=='desk' && $elem['uplink']=='no';
}
$results = array_filter($Unmanaged, 'my_callback');
使用PHP 5.3的匿名函数,您可以在运行时使所选值动态化:
function create_callback($speed, $size, $uplink) {
return function ($elem) use ($speed, $size, $uplink) {
return $elem['speed']==$speed && $elem['size']==$size && $elem['uplink']==$uplink;
};
}
$results = array_filter($unmanaged, create_callback(100, 'desk', 'no'));
编辑:正如您刚评论的那样,您只需要切换名称即可。没问题;使用上述任一解决方案后,您可以添加:
function get_name($elem) { return $elem['name']; }
$names = array_map('get_name', $results);
答案 1 :(得分:1)
如下所示?使用这种方法你可以包括各种条件检查,不要忘记你也可以使用或“||”用于选择一组结果。
foreach($Unmanaged as $result)
{
if($result['speed'] == "100" && $result['size'] == "desk" && $result['uplink'] == "no")
{
echo $result['name'];
}
}
答案 2 :(得分:0)
在阵列上运行的一些选项:
答案 3 :(得分:0)
你去:
function getSwitches($switches,$speed=100,$size='desk',$uplink='no')
{
$sw = array();
foreach($switches as $s)
{
if(
$s['speed'] == $speed &&
$s['size'] == $size &&
$s['uplink'] == $uplink
)
$sw[] = $s;
}
return $sw;
}
print_r(getSwitches($Unmanaged,100,'desk','no'));
答案 4 :(得分:0)
约束可变的另一种选择:
function matches($values, $constraints) {
$hits = array();
foreach($values as $val) {
$ok = true;
foreach($constraints as $k => $v) {
if($val[$k] != $v) $ok = false;
}
if($ok) $hits[] = $val;
}
return $hits;
}
$Unmanaged = array(
array('name' => 'SD2005', 'speed' => '1000', 'size' => 'desk', 'uplink' => 'no'),
array('name' => 'SD2008', 'speed' => '1000', 'size' => 'desk', 'uplink' => 'no'),
array('name' => 'SR2016', 'speed' => '1000', 'size' => 'rack', 'uplink' => 'no'),
array('name' => 'SR2024', 'speed' => '1000', 'size' => 'rack', 'uplink' => 'no'),
array('name' => 'SR2024C', 'speed' => '1000', 'size' => 'rack', 'uplink' => 'no'),
array('name' => 'SD205', 'speed' => '100', 'size' => 'desk', 'uplink' => 'no'),
array('name' => 'SD208', 'speed' => '100', 'size' => 'desk', 'uplink' => 'no'),
array('name' => 'SD216', 'speed' => '100', 'size' => 'desk', 'uplink' => 'no'),
array('name' => 'SR224G', 'speed' => '100', 'size' => 'rack', 'uplink' => 'yes'),
array('name' => 'SR216', 'speed' => '100', 'size' => 'rack', 'uplink' => 'no'),
array('name' => 'SR224', 'speed' => '100', 'size' => 'rack', 'uplink' => 'no'),
array('name' => 'SR224R', 'speed' => '100', 'size' => 'rack', 'uplink' => 'no')
);
print_r(matches($Unmanaged, array('speed'=>'100', 'size'=>'desk', 'uplink'=>'no')));
输出:
Array
(
[0] => Array
(
[name] => SD205
[speed] => 100
[size] => desk
[uplink] => no
)
[1] => Array
(
[name] => SD208
[speed] => 100
[size] => desk
[uplink] => no
)
[2] => Array
(
[name] => SD216
[speed] => 100
[size] => desk
[uplink] => no
)
)
答案 5 :(得分:0)
不妨发布我的解决方案..
function matchElement($haystack, $needleArray) {
$result = array();
for($i = 0, $c = count($haystack); $i < $c; $i++) {
$matchall = true;
foreach($needleArray as $k => $v) {
if(!isset($haystack[$i][$k]) || $haystack[$i][$k] != $v) {
$matchall = false;
break;
}
}
if($matchall) {
$result[] = $haystack[$i];
}
}
return $result;
}
print_r(matchElement($Unmanaged, array("speed" => 100, "size" => "rack")));
答案 6 :(得分:0)
与其他评论相反,我建议使用数据库。这正是数据库的用途,它们可以比php更好。