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

Need to get data value from mysql with array_key_exists

从数据库 table 获取图像数据,在图像列中,我们有 8 个不同尺寸的图像 link 用于单个产品,每个图像 link 都有其尺寸详细信息,例如: - 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

还有更多......

从所有图像中 link 我需要找到 200x200 包含 link for 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'] : '';

您引用了您的 array $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
$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"];

$string=$imageUrlStr;
$array=explode(",",$string);
$pattern = "/200x200/";

?>
    <li class="offer-best-box als-item">
            <div class="offer-best-box-img">
            <a href="<?php echo $rows['productUrl']; ?>" target="_blank"><img src="<?php foreach($array as $value){ $productNewImage =  (preg_match($pattern,$value)) ? $value : ""; echo $productNewImage;} ?>" alt="" /></a>
            </div>
        <div class="offer-best-box-img2">
            <div class="offer-best-box-discount"><span><?php echo round($percentage);  ?>%</span><i>OFF</i></div>
            <div class="offer-best-box-view"><img src="img/store/<?php echo $rows['store']; ?>-small.png" alt="available on store"></div>
        </div>
        <div class="offer-best-box-text"><a href="<?php echo $rows['productUrl']; ?>" target="_blank"><?php echo $rows['title']; ?></a></div>
        <div class="offer-best-box-price">
            <div class="offer-best-box-price-mrp">MRP: Rs. <?php echo $mrpPrice[0]; ?></div>
            <div class="offer-best-box-price-offer">Price: Rs. <?php echo $offerPrice[0]; ?></div>
        </div>
        <div class="mobile-box-button"><a href="<?php echo $rows['productUrl']; ?>" target="_blank">Buy Now</a></div>
</li>