gpt4 book ai didi

php - 按主题或摄影师获取一系列图片过滤

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:23:25 25 4
gpt4 key购买 nike

我有一个包含以下内容的文件 images.json:

[
{
"photographer": "chrysti",
"picture": "ChristyHydeck_1.jpg",
"themes": [
"pets"
]
},
{
"photographer": "chrysti",
"picture": "ChristyHydeck_2.jpg",
"themes": [
"everyday",
"pets"
]
},
{
"photographer": "chrysti",
"picture": "ChristyHydeck_3.jpg",
"themes": [
"outdoors"
]
},
{
"photographer": "jeremy",
"picture": "JeremyVeach_41.jpg",
"themes": [
"everyday",
"pets"
]
}
]

我想根据一些过滤器(传递给函数的参数)搜索项目,这就是我所拥有的:

static function filterImage($photographers = null, $themes = null)
{
$images = array();
$string = file_get_contents( "assets/main/images.json" );

$jsonIterator = new RecursiveIteratorIterator(
new RecursiveArrayIterator( json_decode( $string, true ) ),
RecursiveIteratorIterator::SELF_FIRST
);

foreach ($jsonIterator as $key => $val) {
if (is_array( $val )) {
for ($i = 0; $i < count($val); $i++) {
if (in_array($val[$i], $photographers) || in_array($val[$i], $themes)) {
// code goes here
}
}
} else {
echo "$key => $val\n";
}
}

return $images;
}

我一直在尝试返回一个包含条件匹配的当前 picture 值的数组。让我们将 chrysti 作为 photographer 参数,函数的输出应该是这样的:

$images = ["ChristyHydeck_1.jpg", "ChristyHydeck_2.jpg", "ChristyHydeck_2.jpg"];

现在让我们将 everyday 作为 themes 参数传递,在这种情况下输出应该是:

$images = ["ChristyHydeck_2.jpg", "JeremyVeach_41.jpg"];

如何在条件匹配时获取图片值?任何人都可以给我一些建议来完成这个吗?

最佳答案

试试下面的代码

$images = array();
$string = file_get_contents( "assets/main/images.json" );
$json = json_decode($string, true);
foreach ($json as $key => $val) {
if(in_array($val['photographer'],$photographers)){
$images[] = $val['picture'];
}
foreach ($val['themes'] as $key1 => $val1){
if(in_array($val1,$themes)){
$images[] = $val['picture'];
}
}
}

希望对你有帮助。

关于php - 按主题或摄影师获取一系列图片过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29574700/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com