【问题标题】:How to find the index number of an array that contains a certain key/value如何查找包含某个键/值的数组的索引号
【发布时间】:2013-01-30 17:14:48
【问题描述】:
(
    [1] => Array
        (
            [rules_properties_id] => 1
            [operator] => >=
            [value] => 2
            [function] => NumOrdersPlaced
            [rules_properties_params] => Array
                (
                    [num_days] => 30
                    [customer_id] => 5
                )

        )

    [2] => Array
        (
            [rules_properties_id] => 1
            [operator] => >=
            [value] => 5
            [function] => NumOrdersPlaced
            [rules_properties_params] => Array
                (
                    [num_days] => 90
                    [customer_id] => 5
                )

        )

    [3] => Array
        (
            [rules_properties_id] => 2
            [operator] => >
            [value] => 365
            [function] => CustAcctAge
            [rules_properties_params] => Array
                (
                    [customer_id] => 5
                )

        )

)

这是我从数据库中取回的数组的print_r。我需要找到包含名为 NumOrdersPlaced 的函数的子数组的索引号(预期结果为 2。)这是通过循环遍历数组和子数组并进行比较的唯一方法(如this answer) ?还是有我不知道的更高效、更优雅(即单行)的功能?

【问题讨论】:

  • 你用过array_search吗?
  • @raina77ow - 抱歉,这是我们填充数据库时的剪切和粘贴。实际上永远不会有两个包含相同函数的子数组。但这带来了一个有趣的观点。如果有两个包含相同的内容,我希望它返回一个索引号数组,在本例中为 1 和 2。
  • @AkamOmer - 不,我没有尝试过,因为如上所述,这是一个多维数组。我不想遍历子数组并在每个子数组上调用 array_search。
  • 我可能会在这里使用array_filter grep 带有isset($el['function']) && $el['function'] === 'NumOrdersPlaced' 的所有项目,然后收集结果数组的键。
  • php 不直接支持搜索多维数组。您必须遍历子数组并手动搜索每个子数组。

标签: php arrays multidimensional-array


【解决方案1】:

不,在 PHP 中没有用于搜索多维数组的衬垫,希望您自己为它编写一个函数并将其用作一个衬垫 :)

方法是:

  1. 将数据结构更改为有利于搜索操作的程度。例如带有 xpath 的 xml
  2. 根据您的问题创建数组时,创建另一个数组,其中索引是函数名,而值是指向原始数组的子数组的指针
  3. 使用数据库进行该操作。它已针对它进行了优化
  4. ...

在搜索“正确”方式时,您必须在搜索、插入、更新、删除操作的性能、内存消耗和易用性之间找到一个折衷方案。


正如您要求的 PHP 解决方案,这里有一个示例,我将如何在 PHP 中使用和附加索引数组:(方法 2. 来自上面的列表

// we need two arrays now:
$data = array(); 
$index = array();

// imagine you loop through database query results
foreach($db_result as $record) {
    // create a copy of $record as the address
    // of record will contain the last(!) element agter foreach
    $item = $record;

    // store pointers to that array in data and index
    $data []= &$item;
    $index[$item->function] = &$item;
}


// here is your one liner
$found = isset($index['NumOrdersPlaced']) ? $index['NumOrdersPlaced'] : NULL;

// another index seach:
$found = isset($index['CustAcctAge']) ? $index['CustAcctAge'] : NULL;

// note there is no additonal loop. The cost is the 
// additional memory for $index

【讨论】:

  • 您可以将所有内容放在一行中。它只是不会很可读
  • 您能解释一下$data 数组的用途吗?我看到你正在往里面放东西,但在我能看到的任何地方都无法访问它。
  • 它不是直接访问的,但数据包含“真实”数据。 index 仅包含对数据条目的引用。所以你可以通过'function'直接访问$data的元素
  • @EmmyS 它与数据库中的索引相同。该表包含仅包含对表中记录的引用的数据和索引。这使得搜索操作更快,同时有额外的内存成本(索引)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-18
  • 2017-05-01
  • 2015-01-05
  • 2021-03-17
  • 2016-03-26
相关资源
最近更新 更多