从数据库表中获取图像数据,在图像列中,我们为一个产品提供了 8 个不同尺寸的图像链接,每个图像链接都有其尺寸详细信息,例如:- 100x100、200x200、500x500 等。
所有图像均用逗号指定 喜欢:-
http://example.com/images/data/baby-care-100x100.jpg,
http://example.com/images/data/baby-care-200x200.jpg,
还有更多......
从这个所有图像链接中,我需要找到 200x200 包含 img src 的链接
$productImage = array_key_exists('200x200', '$imageUrlStr')? $imageUrlStr['200x200']:'';
完整代码
$result = $mysqli->query("SELECT * FROM store where store= 'deals' AND bestOffer= '1' AND inStock= 'true' ");
while ($rows = $result->fetch_assoc()) {
$store = $rows["store"];
$productId = $rows["productId"];
$title = $rows["title"];
$description = $rows["description"];
$imageUrlStr = $rows["imageUrlStr"];
$productNewImage = array_key_exists('200x200', $imageUrlStr) ? $imageUrlStr['200x200'] : '';
最佳答案
您已经引用了您的数组
$imageUrlStr
。将您的代码更新为
array_key_exists('200x200', $imageUrlStr) ? $imageUrlStr['200x200'] : '';
您需要知道如何array_key_exists
工作
bool array_key_exists ( mixed $key , array $array )
key Value to check.
array An array with keys to check.
第二个参数必须是数组而不是字符串。所以 $imageUrlStr
必须是数组而不是字符串
编辑:
使用preg_match
$imageUrlStr = "http://example.com/images/data/baby-care-100x200.jpg";
$pattern = '/200x200/';
$productNewImage = preg_match($pattern, $imageUrlStr)) ? $imageUrlStr : '';
您还可以使用strpos
如
$productNewImage = strpos($imageUrlStr, $pattern) !== false) ? $imageUrlStr : '';
关于php - 需要使用 array_key_exists 从 mysql 获取数据值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30456413/