gpt4 book ai didi

php - 通过另一个键和值在多维数组中查找值

转载 作者:可可西里 更新时间:2023-11-01 00:00:25 25 4
gpt4 key购买 nike

我有以下数组:

$array = array(
'item-img-list' => array(
array(
'image-type' => 1,
'image-url' => 'http://img07.allegroimg.pl/...'
),
array(
'image-type' => 2,
'image-url' => 'http://img07.allegroimg.pl/...'
),
array(
'image-type' => 3,
'image-url' => 'http://img07.allegroimg.pl/...'
)
)
)

如何在 'image-type' = '2' 的情况下获取第一个 'image-url' 值?我正在尝试通过这段代码来做到这一点,但什么也没有:

$zdjecia = $item['item-img-list'];
foreach($zdjecia as $zdjecie) {
foreach($zdjecie as $key=>$value) {
if($key == "image-type" && $value == "2") {
$zdjecie_aukcji = $key['image-url'];
}
}
}

感谢您的帮助!

有效!

$searchKey = 2;
foreach($zdjecia as $zdjecie) {
if (**$zdjecie->{'image-type'}** == $searchKey){
$zdjecie_aukcji = **$zdjecie->{'image-url'}**;
break;
}
}

最佳答案

$zdjecia = $item['item-img-list'];
$searchKey = 2;
foreach($zdjecia as $zdjecie) {
if ($zdjecie['image-type'] == $searchKey)
$zdjecie_aukcji = $zdjecie['image-url'];
break;
}
}

或 (PHP >=5.5)

$zdjecia = $item['item-img-list'];
$searchKey = 2;
$results = array_column(
$zdjecia,
'image-url',
'image-type'
);

$zdjecie_aukcji = $results[$searchKey];

关于php - 通过另一个键和值在多维数组中查找值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19704024/

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