在评估MongoDB的PHP类JSON查询 [英] Evaluating MongoDB-like JSON Queries in PHP

查看:154
本文介绍了在评估MongoDB的PHP类JSON查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑这个JSON对象pssed以下(相当复杂)查询前$ P $:

Consider the following (rather complicated) query expressed in this JSON object:

{
    "name": "Kindle Fire",
    "sale": true,
    "price": {
        "$gt": 199,
        "$lt": 264
    },
    "price.vat": { // bogus, just to show $a['price.vat'] == $a['price']['vat']
        "$lte": 1.2
    },
    "$or": {
        "qty": {
            "$gt": 30
        },
        "eta": {
            "$or": {
                "$lt": 3,
                "$gt": 30
            }
        }
    },
    "countriesAvailable": {
        "$in": [
            "US",
            "CA"
        ]
    }
}

目标


我要分析的JSON,使其计算结果为PHP当量(其中 $ A 是我的目标数据):

$a['name'] == 'Kindle Fire' &&
$a['sale'] == true &&
(
    $a['price'] > 199 && $a['price'] < 264
) &&
$a['price']['vat'] <= 1.2 &&
(
    $a['qty'] > 30 ||
    (
        $a['eta'] < 3 || $a['eta'] > 30
    )
) &&
in_array($a['countriesAvailable'], array('US', 'CA'))

我有一点经验的建筑前pression评估。我的想法是遍历从最内层级到最外层的查询,调用相应的 MongoDB的运营商方法为必要的。

假设 $ A 匹配的查询,这将是评估计划:

Assuming $a matches the query, this would be the evaluation plan:

$query = array();
$query['name'] = true;
$query['sale'] = true;
$query['price'] = array();
$query['price']['$gt'] = true;
$query['price']['$lt'] = true;
$query['price']['vat'] = array();
$query['price']['vat']['$lte'] = true;
$query['$or'] = array();
$query['$or']['qty'] = array();
$query['$or']['qty']['$gt'] = false;
$query['$or']['eta'] = array();
$query['$or']['eta']['$or'] = array();
$query['$or']['eta']['$or']['$lt'] = true;
$query['$or']['eta']['$or']['$gt'] = false;
$query['countriesAvailable'] = array();
$query['countriesAvailable']['$in'] = true;

第二步:

$query = array();
$query['name'] = true;
$query['sale'] = true;
$query['price'] = array();
$query['price']['$gt'] = true;
$query['price']['$lt'] = true;
$query['price']['vat'] = true;
$query['$or'] = array();
$query['$or']['qty'] false;
$query['$or']['eta'] = array();
$query['$or']['eta']['$or'] true;
$query['countriesAvailable'] = true;

第三步:

$query = array();
$query['name'] = true;
$query['sale'] = true;
$query['price'] = true;
$query['$or'] = array();
$query['$or']['qty'] false;
$query['$or']['eta'] true;
$query['countriesAvailable'] = true;

第四步:

$query = array();
$query['name'] = true;
$query['sale'] = true;
$query['price'] = true;
$query['$or'] = true;
$query['countriesAvailable'] = true;

由于所有的值是布尔值的评估结束返回!in_array(假,$查询,真实)

如果一个更好的方法存在,让我知道。

If a better approach exists, let me know.

我卡试图让最里面的元素和相关的(忽略运营商)数组索引的路径的,举例来说,如果我用一个 RecursiveIteratorIterator 我得到第一次迭代的正确值:

I'm stuck trying to get the innermost the elements and the relevant (ignoring operators) array index path, for instance, if I use a RecursiveIteratorIterator I get the correct values for the first iteration:

$nodes = new ArrayIterator($query);
$iterator = new RecursiveArrayIterator($nodes);
$iteratorIterator = new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::LEAVES_ONLY);

print_r(iterator_to_array($iteratorIterator));

Array
(
    [name] => Kindle Fire HD
    [sale] => 1
    [$gt] => 30
    [$lt] => 3
    [$lte] => 1.2
    [0] => US
    [1] => CA
)

然而,这是没有多大用处的,因为我不能确定是什么 $ A 的索引键指的,更何况该键值被后者项覆盖而事实上,我不能改变它们的值。

However, it's of little use since I cannot be sure what $a index the keys are referring to, not to mention that the key values are being overwritten by latter entries and the fact that I can't change their values.

我也试过用<一玩href=\"http://php.net/manual/en/class.recursivearrayiterator.php\"><$c$c>RecursiveArrayIterator,但没有 hasParent() / 的getParent()方法似乎不给我过简单的foreach多大的优势ING的数组。

I've also tried playing with RecursiveArrayIterator, but without the hasParent() / getParent() methods it doesn't seem to give me much advantage over simply foreach'ing the array.

有什么建议?

推荐答案

我快速地阅读你的问题听起来像是你要访问叶子,知道他们的关键路径。

I quickly read your question it sounds like you want to visit leafs and know the key path to them.

所以在这里:

$ritit = new RecursiveIteratorIterator(new RecursiveArrayIterator($myArray));
foreach ($ritit as $leafValue) {
    $keyPath = array();
    foreach (range(0, $ritit->getDepth()) as $depth) {
        $keyPath[] = $ritit->getSubIterator($depth)->key();
    }
    // do something with $keyPath


    // or
    $hasParent = $ritit->getDepth() > 0;
    $parentIter = $ritit->getSubIterator($ritit->getDepth() - 1);
    $parentKey = $parentIter->key();
}

这篇关于在评估MongoDB的PHP类JSON查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆