fork download
  1. <?php
  2.  
  3. $arr = array(
  4. array('id' => 'C', 'cCode' => 'BSA', 'cDesc' => 'Bachelor of Science in Accountancy'),
  5. array('id' => 'C', 'cCode' => 'BSCS', 'cDesc' => 'Bachelor of Science in Computer Science'),
  6. array('id' => 'M', 'cCode' => 'MBA', 'cDesc' => 'Master in Business Administration'),
  7. );
  8.  
  9.  
  10. header('Content-type: text/plain');
  11. print_r($arr);
  12.  
  13. $filtered = filterByID('C', $arr);
  14. print_r($filtered);
  15.  
  16. function filterByID($id, $haystack){
  17. $matched = array();
  18. foreach ($haystack as $array) {
  19. if ($array['id'] === $id) {
  20. $matched[] = $array;
  21. }
  22. }
  23.  
  24. return $matched;
  25. }
Success #stdin #stdout 0.03s 52480KB
stdin
Standard input is empty
stdout
Array
(
    [0] => Array
        (
            [id] => C
            [cCode] => BSA
            [cDesc] => Bachelor of Science in Accountancy
        )

    [1] => Array
        (
            [id] => C
            [cCode] => BSCS
            [cDesc] => Bachelor of Science in Computer Science
        )

    [2] => Array
        (
            [id] => M
            [cCode] => MBA
            [cDesc] => Master in Business Administration
        )

)
Array
(
    [0] => Array
        (
            [id] => C
            [cCode] => BSA
            [cDesc] => Bachelor of Science in Accountancy
        )

    [1] => Array
        (
            [id] => C
            [cCode] => BSCS
            [cDesc] => Bachelor of Science in Computer Science
        )

)