Php 需要从mysql获取数据值,且数组\密钥\存在

Php 需要从mysql获取数据值,且数组\密钥\存在,php,mysql,arrays,image,array-key-exists,Php,Mysql,Arrays,Image,Array Key Exists,从数据库表获取图像数据,在图像列中,我们有8个不同尺寸的图像链接,用于单个产品,每个图像链接都有其尺寸详细信息,如:-100x100、200x200、500x500等 所有图像都用逗号指定 比如:- , , , 还有更多 从这个“所有图像”链接中,我需要找到200x200包含img src的链接 $productImage = array_key_exists('200x200', '$imageUrlStr')? $imageUrlStr['200x200']:''; 完整代码 $resu

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

所有图像都用逗号指定 比如:-

,

,

,

还有更多

从这个“所有图像”链接中,我需要找到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'] : '';
你需要知道它是如何工作的

要检查的值

阵列 带有要检查的键的数组

第二个参数必须是数组而不是字符串。因此,
$imageUrlStr
必须是数组而不是字符串

编辑: 使用

您还可以使用


仍然不工作,面临错误警告:array\u key\u exists()期望参数2是数组,显示更多代码时给出的字符串,显然$imageUrlStr不是您认为的那样,或者它没有通过数据库查询从变量名加载,看起来
$imageUrlStr
是字符串,而不是数组。因此,调用
array\u key\u存在于其上是没有意义的。$result=$mysqli->query(“从存储中选择*,其中存储='deals'和bestOffer='1'和inStock='true');而($rows=$result->fetch_assoc()){$store=$rows[“store”];$productId=$rows[“productId”];$title=$rows[“title”];$description=$rows[“description”];$imageUrlStr=$rows[“imageUrlStr”];$productNewImage=数组密钥存在('200x200',$imageUrlStr)$imageUrlStr['200x200']:'';您能帮助我如何对preg_match进行更正以获得200x200图像链接首先添加
$imageUrlStr
的值。然后只能给出解决方案。谢谢。我是从数据库中获取其值的。它的值与每个产品不同,所以我该怎么做?请显示更多代码。我们需要了解您是如何设置的
$imageUrlStr
。只需将其打印出来。如果它是一个数组,则
打印($imageUrlStr)
。否则回显
$imageUrlStr
。或者它是一个接一个动态出现的?匹配它的字符串不是您需要使用的数组,而不是
数组\u键
bool array_key_exists ( mixed $key , array $array )
$imageUrlStr = "http://example.com/images/data/baby-care-100x200.jpg";
$pattern = '/200x200/';
$productNewImage = preg_match($pattern, $imageUrlStr)) ? $imageUrlStr : '';
$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>