(
[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 })?或者是否有一种我不了解的更有效,更优雅(即单行)的功能?
答案 0 :(得分:0)
不,在PHP中没有一个用于搜索多维数组的线程,期望你自己编写一个函数并将其用作一个线程:)
方法是:
在搜索“正确”方式时,您必须在搜索,插入,更新,删除操作,内存消耗和易用性之间找到折衷方案。
当您要求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