0

My initial array is

$employees = array(
array('name' => 'jack',
      'area' => 'crafts'),

array('name' => 'janet',
      'area' => 'aquatics'),

array('name' => 'brad',
      'area' => 'crafts')
);

I am trying to create a new array based on the search results of another array so the new array should look like this if I search for 'crafts':

$employees2 = array(
array('name' => 'jack',
      'area' => 'crafts'),

array('name' => 'brad',
      'area' => 'crafts')
);

What is the simplest solution I can do get get this new result.

John Conde
  • 217,595
  • 99
  • 455
  • 496
user2108555
  • 85
  • 1
  • 7
  • What's the criteria for deciding what goes in the new array? – John Conde Feb 25 '13 at 19:49
  • http://stackoverflow.com/questions/8440073/filter-array-by-key – Martin Zeitler Feb 25 '13 at 19:53
  • or how about http://www.php.net/manual/en/language.types.array.php ? – Martin Zeitler Feb 25 '13 at 19:55
  • I have tried to use the first answer from this question. http://stackoverflow.com/questions/6661530/php-multi-dimensional-array-search – user2108555 Feb 25 '13 at 20:01
  • But it only returns one key. I need it to return multiple keys and create a new array from the results. – user2108555 Feb 25 '13 at 20:02
  • I will try these answers later tonight and let you all know how it goes. Thank you very much. – user2108555 Feb 25 '13 at 20:04
  • I think I should go a little more In depth to how I want the results to turn out. I am creating an include file with all of this information. There will only be about 75 different entries. There are several different pages (aquatics, crafts, commissary, admin and several more) which I want to pull the information from the include file. For each page, I want the data that matches for each area to be displayed in a table. So the employees from each area are displayed on their area's respective areas. – user2108555 Feb 25 '13 at 20:52

5 Answers5

1
foreach($employees as $key => $value){

    if($value['area']=='crafts'){
        $employees2[] = $value;
    }

}

This quite simply loops through the first array and checks the value of "area" in the internal array. If the value is equal to "crafts" you can then put that into a new array which is called $employees2. You can change crafts to whatever you want and add anything you want between the [ ] in employees2 if you wish to customise the key.

James
  • 2,013
  • 3
  • 18
  • 31
  • I have tried this method and I get an error "employees2 undefined." – user2108555 Feb 25 '13 at 23:40
  • Ha! I got this to work. It usually helps if I move the include link above this code otherwise the script doesn't know where to find the data. How silly of me. Thank you very much for your help. You saved my terrible day. – user2108555 Feb 26 '13 at 05:59
  • @user2108555 If you found the answer to be correct, could you please mark it as the correct answer. Thank you. – James Feb 26 '13 at 08:37
  • It won't let me because it says I don't have enough "Reputation". Sorry – user2108555 Feb 26 '13 at 18:21
0

Try this:

$employees = array(
array('name' => 'jack',
  'area' => 'crafts'),

array('name' => 'janet',
  'area' => 'aquatics'),

array('name' => 'brad',
  'area' => 'crafts')
);

$employees2 = array();

foreach ($employees as $key) {
if($key['name'] == "jack")
{
    array_push($employees2,array('name'=>$key['name'],'area'=>$key['area']));
}
}

var_dump($employees2);

The array_push do all the trick ;)

Saludos.

Hackerman
  • 12,139
  • 2
  • 34
  • 45
0

You could simplify the syntax (but not the algorythmic complexity) by using a utility-belt library Underscore.php (http://brianhaveri.github.com/Underscore.php/)

There's a number of array-"plucking" methods that saves you the need to write loops, but under the bonnet it does much of the same as decribed in answers above.

Val Redchenko
  • 580
  • 1
  • 8
  • 20
0

I will assume that the possible result set can be large. In which case you would want to process the array with as little extra memory as possible. For this I suggest iterating through the array by reference and unsetting the items that do not match your criteria. Possibly less overhead than creating a new array to store the items that match your filter. Then you can check if the array is empty or not to determine if the filter returns any results. Like so:

<?php
// maybe this will be set through an option from the UI
$area_filter = 'crafts';

// fetched results
$employees = array(
    array('name' => 'jack',
          'area' => 'crafts'),

    array('name' => 'janet',
          'area' => 'aquatics'),

    array('name' => 'brad',
          'area' => 'crafts')
);

// filter out the items that match your filter
foreach($employees as $i => &$employee){
    if($employee['area'] != $area_filter){
        unset($employees[$i]);
    }
}

// do something with the results
if(!empty($employees)){
    print_r($employees);
} else {
    echo "Sorry, your filter '$area_filter' did not match any results\n";
}
?>
EmmanuelG
  • 1,051
  • 9
  • 14
0

Try this :

$employees = array(
array('name' => 'jack',
  'area' => 'crafts'),
array('name' => 'janet',
  'area' => 'aquatics'),
array('name' => 'brad',
  'area' => 'crafts')
);

$employees       = array_filter($employees, function($employee) {
   return ($employee['area'] == 'crafts' );
});
echo "<pre>";
print_r($employees);
Prasanth Bendra
  • 31,145
  • 9
  • 53
  • 73