在数组中搜索相关信息

发布于 2024-12-10 01:23:07 字数 9681 浏览 0 评论 0原文

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

    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 个实例(如果它们存在于数组中)。

I have an array that looks like this,

    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
        )

)

I want to search through the array and if for example a production_type = "television" exists I want to show some HTML.

Can I do this without looping through the array?

In essence I want to do the following,

if key and value exists anywhere in array  
    do something;

I am in the end wanting to construct a list of production_types but only list 1 instance of each type if they exist in the array.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

浮萍、无处依 2024-12-17 01:23:07

使用原生函数 array_filter 的单次调用:

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

Using a single call of the native function array_filter:

$tvInThere = array_filter(function ($e) {
  return isset($e['production_type']) &&  $e['production_type'] == 'television';
}, $arr) > 0;
此生挚爱伱 2024-12-17 01:23:07

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

You can use the php function array_key_exists() to determine if a key is in an array. Similarly, the function in_array() will output 'true' if a value is in an array. However, these functions do not work recursively. You will therefore have to do some manner of looping.

困倦 2024-12-17 01:23:07

如果您想避免迭代数组,则可以对数据使用错误的构造。

//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 这样的系统更加明显,该函数无法优化,必须进行优化在运行时按原样执行。

You use a wrong construct for your data if you want to avoid iterating the array.

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

EDIT: Why array_filter isnt the best solution

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

Iterates over each value in the input array passing them to the callback function. If the callback function returns true, the current value from input is returned into the result array. Array keys are preserved.

the array_filter iterates over each value, and thus will be slower on bigger sets, because it does not stop on the first hit.

And secondly since you dont want to iterate the set, assuming it is for performance, the anonymous callback function used in the array_filter method is a lot slower, this difference is more noticeable with systems like apc_cache, the function cannot be optimized and has to be executed as-is at runtime.

暖风昔人 2024-12-17 01:23:07
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 我只是循环,我想性能会比我展示的上面的例子更好。

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

Just another way of doing the same thing... FWIW I would just loop though, I'd imagine the performance would be better than the above example I've shown.

泼猴你往哪里跑 2024-12-17 01:23:07

你所说的需要循环。在您的伪代码中,“任意位置”本质上是一个循环:)。

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

如果你的数组大小合理,单个循环不会杀死 - 所以如果你担心效率,请不要担心:)。

What you're talking about requires looping. In your pseudocode the "anywhere in" is essentially a loop :).

If you want to avoid looping for some specific reason, you could do something like redundantly store you're data in dictionary type structures with useful keys - but this is not advisable.

If you're arrays are of reasonable size, a single loop isn't going to kill - so if you are worried about efficiency dont fret :).

七七 2024-12-17 01:23:07

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

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

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

  // output...
}

UPDATE

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

While there are libraries that can do this, there is no single, native PHP function for a multidimensional array search.

However, it seems to me you'd have to loop over the array for output anyway. In which case, what's the problem with:

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

  // output...
}

UPDATE

array_filter() does work in this case. Thanks phihag.

度的依靠╰つ 2024-12-17 01:23:07

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

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

Similar to (but not the same as) array_filter you can solve the generic problem with a FilterIterator.

I've created a generic one that takes an array to iterate over and a callback function.

Iterators can be used in foreach and they behave pretty much the same like an array. A FilterIterator return only a subset of the elements, but the elements are not deleted:

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

Iterators are very powerful, because you can more easily change your code later on by just switching the filter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文