简体   繁体   中英

php create a new array from search results of another array

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.

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.

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.

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.

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";
}
?>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM