php - 需要使用 array_key_exists 从 mysql 获取数据值

标签 php mysql arrays image array-key-exists

从数据库表中获取图像数据,在图像列中,我们为一个产品提供了 8 个不同尺寸的图像链接,每个图像链接都有其尺寸详细信息,例如:- 100x100、200x200、500x500 等。

所有图像均用逗号指定 喜欢:-

http://example.com/images/data/baby-care-100x100.jpg,

http://example.com/images/data/baby-care-200x200.jpg,

http://example.com/images/data/baby-care-250x250.jpg,

http://exampple.com/images/data/baby-care-500x500.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/

相关文章:

php - 使用php和mysql获取并存储根目录和文件

javascript - 由两个 div 制成的条形图

c# - MySQL Query 和 DataGridView - 关联具有相同 ID 的数据

java - 尝试使用 ArrayList 操作数组中的对象

php - 将数据放入规范化数据库中

PHP - 将 MySQL 中的所选项目保存在 $_SESSION 中

php - 拉拉维尔 5.1。无法运行 artisan 命令

c - 指向 char 数组的指针的值与普通 char 指针的值

arrays - Fortran:类型未知大小的数组

php - 在 PHP Smarty 模板引擎上禁用缓存?