Php 在数组中搜索相关信息

Php 在数组中搜索相关信息,php,arrays,codeigniter,Php,Arrays,Codeigniter,我有一个像这样的数组 Array ( [0] => Array ( [career_overview_id] => 1 [production_title] => Televison Show [production_role] => Role [production_director] => Director [p

我有一个像这样的数组

    Array
(
    [0] => Array
        (
            [career_overview_id] => 1
            [production_title] => Televison Show
            [production_role] => Role
            [production_director] => Director
            [production_company] => Production Company
            [production_producer_theartre] => 
            [production_project_type] => 
            [production_product] => 
            [production_agency] => 
            [production_type] => 
            [candidates_candidate_id] => 37
        )

    [1] => Array
        (
            [career_overview_id] => 2
            [production_title] => Televison Show
            [production_role] => Role
            [production_director] => Director
            [production_company] => Production Company
            [production_producer_theartre] => 
            [production_project_type] => 
            [production_product] => 
            [production_agency] => 
            [production_type] => 
            [candidates_candidate_id] => 37
        )

    [2] => Array
        (
            [career_overview_id] => 3
            [production_title] => Televison Show
            [production_role] => Role
            [production_director] => Director
            [production_company] => Production Company
            [production_producer_theartre] => 
            [production_project_type] => 
            [production_product] => 
            [production_agency] => 
            [production_type] => 
            [candidates_candidate_id] => 37
        )

    [3] => Array
        (
            [career_overview_id] => 4
            [production_title] => Televison Show
            [production_role] => Role
            [production_director] => Director
            [production_company] => Production Company
            [production_producer_theartre] => 
            [production_project_type] => 
            [production_product] => 
            [production_agency] => 
            [production_type] => voice over
            [candidates_candidate_id] => 37
        )

    [4] => Array
        (
            [career_overview_id] => 5
            [production_title] => Televison Show
            [production_role] => Role
            [production_director] => Director
            [production_company] => Production Company
            [production_producer_theartre] => 
            [production_project_type] => 
            [production_product] => 
            [production_agency] => 
            [production_type] => voice over
            [candidates_candidate_id] => 37
        )

    [5] => Array
        (
            [career_overview_id] => 6
            [production_title] => Televison Show
            [production_role] => Role
            [production_director] => Director
            [production_company] => Production Company
            [production_producer_theartre] => 
            [production_project_type] => 
            [production_product] => 
            [production_agency] => 
            [production_type] => voice over
            [candidates_candidate_id] => 37
        )

    [6] => Array
        (
            [career_overview_id] => 7
            [production_title] => Televison Show
            [production_role] => Role
            [production_director] => Director
            [production_company] => Production Company
            [production_producer_theartre] => 
            [production_project_type] => 
            [production_product] => 
            [production_agency] => 
            [production_type] => voice over
            [candidates_candidate_id] => 37
        )

    [7] => Array
        (
            [career_overview_id] => 8
            [production_title] => Televison Show
            [production_role] => Role
            [production_director] => Director
            [production_company] => Production Company
            [production_producer_theartre] => 
            [production_project_type] => 
            [production_product] => 
            [production_agency] => 
            [production_type] => voice over
            [candidates_candidate_id] => 37
        )

    [8] => Array
        (
            [career_overview_id] => 9
            [production_title] => Televison Show
            [production_role] => Role
            [production_director] => Director
            [production_company] => Production Company
            [production_producer_theartre] => 
            [production_project_type] => 
            [production_product] => 
            [production_agency] => 
            [production_type] => voice over
            [candidates_candidate_id] => 37
        )

    [9] => Array
        (
            [career_overview_id] => 10
            [production_title] => Production Title
            [production_role] => Role
            [production_director] => Director
            [production_company] => Production Company
            [production_producer_theartre] => 
            [production_project_type] => 
            [production_product] => 
            [production_agency] => 
            [production_type] => television
            [candidates_candidate_id] => 37
        )

    [10] => Array
        (
            [career_overview_id] => 11
            [production_title] => Production Title
            [production_role] => Role
            [production_director] => Director
            [production_company] => Production Company
            [production_producer_theartre] => 
            [production_project_type] => 
            [production_product] => 
            [production_agency] => 
            [production_type] => television
            [candidates_candidate_id] => 37
        )

    [11] => Array
        (
            [career_overview_id] => 12
            [production_title] => Production Title
            [production_role] => Role
            [production_director] => Director
            [production_company] => Production Company
            [production_producer_theartre] => 
            [production_project_type] => 
            [production_product] => 
            [production_agency] => 
            [production_type] => television
            [candidates_candidate_id] => 37
        )

    [12] => Array
        (
            [career_overview_id] => 13
            [production_title] => Production Title
            [production_role] => Role
            [production_director] => Director
            [production_company] => Production Company
            [production_producer_theartre] => 
            [production_project_type] => 
            [production_product] => 
            [production_agency] => 
            [production_type] => television
            [candidates_candidate_id] => 37
        )

    [13] => Array
        (
            [career_overview_id] => 14
            [production_title] => Production Title
            [production_role] => Role
            [production_director] => Director
            [production_company] => Production Company
            [production_producer_theartre] => 
            [production_project_type] => 
            [production_product] => 
            [production_agency] => 
            [production_type] => television
            [candidates_candidate_id] => 37
        )

    [14] => Array
        (
            [career_overview_id] => 15
            [production_title] => Production Title
            [production_role] => Role
            [production_director] => Director
            [production_company] => Production Company
            [production_producer_theartre] => 
            [production_project_type] => 
            [production_product] => 
            [production_agency] => 
            [production_type] => television
            [candidates_candidate_id] => 37
        )

    [15] => Array
        (
            [career_overview_id] => 16
            [production_title] => Production Title
            [production_role] => Role
            [production_director] => Director
            [production_company] => Production Company
            [production_producer_theartre] => 
            [production_project_type] => 
            [production_product] => 
            [production_agency] => 
            [production_type] => television
            [candidates_candidate_id] => 37
        )

    [16] => Array
        (
            [career_overview_id] => 17
            [production_title] => Production Title
            [production_role] => Role
            [production_director] => Director
            [production_company] => Production Company
            [production_producer_theartre] => 
            [production_project_type] => 
            [production_product] => 
            [production_agency] => 
            [production_type] => theartre
            [candidates_candidate_id] => 37
        )

)
我想搜索整个数组,例如,如果存在一个production_type=television,我想显示一些HTML

我可以在不循环数组的情况下执行此操作吗

本质上我想做以下几点

if key and value exists anywhere in array  
    do something;

最后,我想构建一个生产类型列表,但如果数组中存在,则只列出每种类型的一个实例。

虽然有库可以做到这一点,但没有用于多维数组搜索的单一本机PHP函数

然而,在我看来,无论如何,您都必须循环数组以获得输出。在这种情况下,有什么问题:

foreach ($items as $item) {
  if ($item['production_type'] != 'television') {
    continue;
  }

  // output...
}
使现代化
在这种情况下,数组过滤器不起作用。感谢phihag。

使用本机函数的单个调用:


您可以使用php函数array_key_exists来确定某个键是否在数组中。类似地,如果数组中有值,则_数组中的函数将输出“true”。但是,这些函数不是递归工作的。因此,您必须执行某种方式的循环

foreach ($infoChunk in $yourBigAssArray){
    $search1 = array_search('Television Show', $infoChunk);
    $search2 = array_search('Mr Pink', $infoChunk);
    if ($search1 && $search2 
        && $search1 =='production_title' 
        && $search2 =='production_director'
    ){
        doShiz();
    }
}

只是做同样事情的另一种方式。。。FWIW如果我只是循环,我想性能会比上面我展示的示例更好。

您所说的需要循环。在伪代码中,anywhere In本质上是一个循环:

如果您想避免由于某些特定原因而出现循环,您可以使用有用的键在字典类型的结构中冗余存储数据,但这是不可取的


如果您希望阵列的大小合理,那么单个循环不会导致死亡-因此,如果您担心效率,请不要担心:

如果要避免迭代数组,则对数据使用错误的构造

//function to search the set and stop on first hit
function exists_in_set (&$set, $key, $value) {
    foreach ($set as $row) {
        if (isset($row[$key]) && $row[$key] = $value)
            return true;
    }
    return false;
}

//now for your 
//  if key and value exists anywhere in array  
//      do something;
if (exists_in_set($set, $key, $value)) {
    //do something
}
编辑:为什么阵列过滤器不是最佳解决方案

资料来源:

迭代输入数组中的每个值,并将其传递给回调函数。如果回调函数返回true,则输入的当前值将返回到结果数组中。保留数组键

数组_过滤器迭代每个值,因此在较大的集合上速度会较慢,因为它不会在第一次命中时停止


第二,由于您不想迭代集合,假设它是为了性能,array_filter方法中使用的匿名回调函数要慢得多,这种差异在apc_cache之类的系统中更为明显,该函数无法优化,必须在运行时按原样执行。

与array_filter类似,但与array_filter不同。您可以使用

我已经创建了一个泛型函数,它需要一个数组进行迭代,还需要一个回调函数

迭代器可以在foreach中使用,它们的行为与数组几乎相同。FilterEditor仅返回元素的子集,但不会删除元素:

class ArrayFilterIterator extends FilterIterator
{
    private $it, $filter;
    public function __construct(array $array, $filter)
    {
        $this->it = new ArrayIterator($array);
        $this->filter = $filter;
        parent::__construct($this->it);
    }
    public function accept()
    {
        $func = $this->filter;
        return $func($this->it->current());
    }
}

$filter = function($v){ return $v['production_type'] == "television";};

$it = new ArrayFilterIterator($array, $filter);

foreach($it as $entry)
{
    print_r($entry);
}

迭代器非常强大,因为您以后只需切换过滤器,就可以更轻松地更改代码。

我假设downvoter对我的第一条语句有问题。请参阅更新。我仍然觉得,如果用户正在循环显示,那么预过滤阵列是没有意义的。+1同意,我在这方面也得到了纠正。这是一个更好的解决方案。
class ArrayFilterIterator extends FilterIterator
{
    private $it, $filter;
    public function __construct(array $array, $filter)
    {
        $this->it = new ArrayIterator($array);
        $this->filter = $filter;
        parent::__construct($this->it);
    }
    public function accept()
    {
        $func = $this->filter;
        return $func($this->it->current());
    }
}

$filter = function($v){ return $v['production_type'] == "television";};

$it = new ArrayFilterIterator($array, $filter);

foreach($it as $entry)
{
    print_r($entry);
}