简体   繁体   English

php从另一个数组的搜索结果创建一个新数组

[英]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 ;) array_push可以解决所有问题;)

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. 这很简单地循环遍历第一个数组,并检查内部数组中“ area”的值。 If the value is equal to "crafts" you can then put that into a new array which is called $employees2. 如果该值等于“ crafts”,则可以将其放入一个名为$ 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. 如果希望自定义密钥,可以将工艺更改为所需的任何内容,并在employees2中的[]之间添加所需的任何内容。

You could simplify the syntax (but not the algorythmic complexity) by using a utility-belt library Underscore.php ( http://brianhaveri.github.com/Underscore.php/ ) 您可以使用实用程序皮带库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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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