i am using jquery query builder , $rule is array how to loop thround all inner arrays with their proper condition and returns the $rule is true or false?
$rule = array (
'condition' => 'AND',
'rules' => array (
0 => array (
'id' => 'name',
'field' => 'name',
'type' => 'string',
'input' => 'text',
'operator' => 'equal',
'value' => 'bibin',
),
1 => array (
'id' => 'category',
'field' => 'category',
'type' => 'integer',
'input' => 'select',
'operator' => 'not_equal',
'value' => 1,
),
2 => array (
'condition' => 'OR',
'rules' => array (
0 => array (
'id' => 'name',
'field' => 'name',
'type' => 'string',
'input' => 'text',
'operator' => 'equal',
'value' => 'john',
),
1 => array (
'id' => 'category',
'field' => 'category',
'type' => 'integer',
'input' => 'select',
'operator' => 'equal',
'value' => 2,
),
2 => array (
'condition' => 'OR',
'rules' => array (
0 => array (
'id' => 'name',
'field' => 'name',
'type' => 'string',
'input' => 'text',
'operator' => 'equal',
'value' => 'tech',
),
1 => array (
'id' => 'price',
'field' => 'price',
'type' => 'double',
'input' => 'number',
'operator' => 'greater_or_equal',
'value' => 500,
),
),
),
3 => array (
'condition' => 'AND',
'rules' => array (
0 => array (
'id' => 'name',
'field' => 'name',
'type' => 'string',
'input' => 'text',
'operator' => 'equal',
'value' => 'top',
),
1 => array (
'id' => 'category',
'field' => 'category',
'type' => 'integer',
'input' => 'select',
'operator' => 'equal',
'value' => 5,
),
),
),
),
),
3 => array (
'condition' => 'AND',
'rules' => array (
0 => array (
'id' => 'name',
'field' => 'name',
'type' => 'string',
'input' => 'text',
'operator' => 'equal',
'value' => 'vishnu',
),
1 => array (
'id' => 'price',
'field' => 'price',
'type' => 'double',
'input' => 'number',
'operator' => 'less_or_equal',
'value' => 1000,
),
),
),
)
);
I have a nested array called $rule. I want to check the array returns true or false Is any idea to check this array returns true or false? How to iterarte and check with inner arrays?
You have a logical operator applying to some rules.
You might need a function that uses that operator on the differents rules.
I assume you build it that way
/*
* $operator being 'OR' / 'AND'
* $rules being an array
* returns true or false
*/
function ApplyCondition($operator, $rules)
{
// some code
}
I suggest you to build a function that will loop through the rules and check if the $rules array has a 'condition'
key. Since your original array is a tree (you don't know how deep can be the nested arrays), you must use a recursive function to parse it.
/*
* $rules being an array
* At first call, you have no condition yet, hence its default value is null
* returns true or false
*/
function ParseRules($rules, $condition = null)
{
if (isset($rules['condition']) && isset($rules['rules']))
{
/*
* An array with some rules and a condition was found.
* Let's do a recursive call that will return true or false
*/
return (ParseRules($rules['rules'], $rules['condition']));
}
else
{
/*
* An array without a condition was found.
* Let's apply the upper function that will return true or false
* to the previous recursive call
*/
return (ApplyCondition($condition, $rules));
}
}
/*
* $operator being 'OR' / 'AND'
* $rules being an array
* returns true or false
*/
function ApplyCondition($operator, $rules)
{
foreach ($rules as $rule)
{
if (isset($rule['condition']) && isset($rule['rules']))
{
/*
* An array with some rules and a condition was found.
* Let's do a recursive call that will return true or false
*/
$SomeBooleanValueToHandle = ParseRules($rule['rules'], $rule['condition']);
}
else
{
// some code
}
}
return ($SomeBooleanValueToHandle);
}
U can use foreach loop to access every nested array:
echo "<pre>";
foreach ($rule as $r) {
var_dump($r);
}
If u want to access some property (key => value), u can do it with $r['id'];
$r['field'];
.. etc.