在数组中搜索相关信息

时间:2011-10-14 13:54:58

标签: php arrays codeigniter

我有一个看起来像这样的数组,

    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;

我最终希望构建一个production_types列表,但只列出每个类型的1个实例(如果它们存在于数组中)。

7 个答案:

答案 0 :(得分:2)

使用本机函数array_filter的单次调用:

$tvInThere = array_filter(function ($e) {
  return isset($e['production_type']) &&  $e['production_type'] == 'television';
}, $arr) > 0;

答案 1 :(得分:1)

您可以使用php函数array_key_exists()来确定某个键是否在数组中。类似地,如果值在数组中,函数in_array()将输出'true'。但是,这些函数不能递归地工作。因此,您必须采取某种方式进行循环。

答案 2 :(得分:1)

如果要避免迭代数组,请为数据使用错误的构造。

//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
}

编辑:为什么array_filter不是最佳解决方案

来源:http://php.net/manual/en/function.array-filter.php

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

array_filter迭代每个值,因此在较大的集合上会更慢,因为它不会在第一次打击时停止。

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

答案 3 :(得分:0)

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

但是,在我看来,无论如何你都必须遍历数组以进行输出。在这种情况下,问题是什么:

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

  // output...
}

更新

array_filter()在这种情况下有效。谢谢 phihag

答案 4 :(得分:0)

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我会循环,但我认为性能会优于上面我展示的例子。

答案 5 :(得分:0)

您所谈论的内容需要循环播放。在你的伪代码中,“任何地方”本质上是一个循环:)。

如果由于某些特定原因想要避免循环,你可以做一些事情,比如冗余地将数据存储在带有用键的字典类型结构中 - 但这是不可取的。

如果你的阵列大小合理,单个循环就不会被杀 - 所以如果你担心效率不要担心:)。

答案 6 :(得分:0)

array_filter类似(但不相同),您可以使用FilterIterator解决一般问题。

我创建了一个通用的数组迭代和回调函数。

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

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

Demo

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