php - 根据数组值动态放置数组条件
问题描述
我有下面的数组。我想根据即将到来的部分动态设置条件。
stdClass Object
(
[content_form] => Array
(
[0] => genre
[1] => cast
[2] => category
)
[content_type] => stdClass Object
(
[genre] => Array
(
[0] => History
[1] => ACTION
[2] => ROMANTIC
)
[cast] => Array
(
[0] => 13128
[1] => 13127
)
[category] => Array
(
[0] => 4119
[1] => 4118
[2] => 4081
)
)
[conditions] => Array
(
[0] => OR
[1] => AND
)
)
在上面的 content_form 数组中,有 3 个数组值,在第 0 个索引类型中,存在第 1 个索引类型和第 2 个索引类别。
类似地,对于 content_type,存在 3 个数组值,并且在最后一个数组条件中存在。
我的要求是,根据接下来的部分,我想对他们提出条件。示例-(((流派 OR 演员)AND 类别)
同样,如果我的输出如下所示,则条件将是 Example - (genre OR cast)
[content_form] => Array
(
[0] => genre
[1] => cast
)
[content_type] => stdClass Object
(
[genre] => Array
(
[0] => History
[1] => ACTION
[2] => ROMANTIC
)
[cast] => Array
(
[0] => 13128
[1] => 13127
)
)
[conditions] => Array
(
[0] => OR
)
)
如果我的输出如下所示,那么任何其他键都不会出现任何条件,它将返回简单的结果。
stdClass Object
(
[content_form] => Array
(
[0] => cast
)
[content_type] => stdClass Object
(
[cast] => Array
(
[0] => 13128
[1] => 13127
)
)
[conditions] => Array
(
)
)
我如何根据内容类型和条件数组按照顺序来动态执行此操作?
解决方案
我们已经做到了。请检查:
$jsonData = '{"content_form":["genre","cast","category", "prakash"],"content_type":{"genre":["History","ACTION","ROMANTIC"],"cast":["13128","13127"],"category":["4119","4118","4081"]},"conditions":["OR","AND","OR"]}';
$contentIds = array(
'genre'=>array(1,2,3),
'cast'=>array(1,2,4),
'category'=>array(3,2,7),
'prakash'=>array(1,2,3,8,9)
);
$arrayDecode = json_decode($jsonData,true);
$result = array();
foreach($arrayDecode['conditions'] as $key=>$val){
if($result){
$secondArr = $contentIds[$arrayDecode['content_form'][$key+1]];
$result = array_merge($result, $secondArr);
} else {
$firstArr = $contentIds[$arrayDecode['content_form'][$key]];
$secondArr = $contentIds[$arrayDecode['content_form'][$key+1]];
$result = array_merge($firstArr, $secondArr);
}
if($val=='OR'){
$result = array_unique($result);
} else {
$result = array_diff_assoc($result, array_unique($result));
}
}
$result = array_values($result);
print_r($result);
让我知道这是否是您的要求?
推荐阅读
- c - 位掩码 - 左移溢出
- javascript - 如何使用javascript将值传递给文本字段
- laravel - 翻译“给定的数据无效。” 在 Laravel 5.6 中
- azure - 尝试调用虚方法...关于空对象引用问题
- java - 我可以将 long 类型的一维数组元素类型转换为 int 吗?
- php - 更改单个元素数组的键
- c++ - c++ 提升范围。数组中的对数组
- ios - WKWebView 无法加载某些图片
- python - Django Rest Framework ViewSet 的附加视图
- jupyter-notebook - 取消 jupyter notebook 在工具栏显示中文