tags:

views:

81

answers:

7

Hi all,

If I have the following array:

$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')
                );

how would I cycle through all entries returning all the entries that meet a certain criteria for example: all switches that are speed:100, size:desk and uplink:no ?

Please help.

Thanks, Ben

+1  A: 

Something like the following? Using this method you could include all manner of condition checking and don't forget you could also use or "||" for selecting a set of results.

foreach($Unmanaged as $result)
{
    if($result['speed'] == "100" && $result['size'] == "desk" && $result['uplink'] == "no")
    {
     echo $result['name'];
    }

}
Sam152
+5  A: 

Here's how you could do it using array_filter():

function my_callback($elem) {
    return $elem['speed']==100 && $elem['size']=='desk' && $elem['uplink']=='no';
}

$results = array_filter($Unmanaged, 'my_callback');

Using PHP 5.3's anonymous functions, you can make the chosen values dynamic at runtime:

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'));

Edit: As you just commented, you just want the name of the switch. No problem; after using either of the solutions above you could add:

function get_name($elem) { return $elem['name']; }
$names = array_map('get_name', $results);
Ben James
Sorry for not being clear - all I want returned is the name of the switch....
Bift
A: 

some option to run on the array:

  1. foreach($array as $key=>$val)
  2. while (list($key, $val) = each($array)) {
  3. array_walk($array,func)
Haim Evgi
A: 

here you go:

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'));
Marcin
A: 

Another option where the constraints are variable:

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')));

Output:

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
        )

)
Bart Kiers
A: 

Might as well post my solution..

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")));
Les
A: 

On the contrary to other comments, i'd suggest using a database. This is exactly what databases are for and they can this a waaaay better than php.

stereofrog