【问题标题】:empty the old value of $array[] in php清空 php 中 $array[] 的旧值
【发布时间】:2017-09-24 06:40:01
【问题描述】:

我正在尝试删除我在互联网上搜索的数组的旧值我刚刚找到了使用此solution 的方法 unset() 但我的情况似乎有所不同,这是我的完整代码

<?php



header('Content-type: application/json; charset=utf-8');
$link = mysqli_connect("localhost","root","");
mysqli_query($link,"SET NAMES UTF8");
$db= mysqli_select_db($link,"Mirsa") or die("Cannot select Database.");



 if (isset($_GET['Product_ID']) ){
   $productId;
    $Menu_ID =$_GET['Product_ID'];



$query = "SELECT 
   mirsaProductId,mirsaProductCode,mirsaProductDescription,
   mirsaProductPcsInCartoon,mirsaProductWeight,mirsaProductVolume,
    mirsaProductCodeBType,mirsaProductCodeCType,FK_mirsaCategoryId 
   from mirsaProduct WHERE FK_mirsaCategoryId='$Menu_ID'";



    $result = mysqli_query($link,$query) or die(mysql_error($link));

                $response["Products"] = array();
                $products = array();
         while ($row = mysqli_fetch_array($result)) {
                $image[]=array()         
                $products["mirsaProductId"] =          $row["mirsaProductId"];
                $products["mirsaProductCode"]         = $row["mirsaProductCode"];
                $products["mirsaProductDescription"]  = $row["mirsaProductDescription"];
                $products["mirsaProductPcsInCartoon"] = $row["mirsaProductPcsInCartoon"];
                $products["mirsaProductWeight"] =      $row["mirsaProductWeight"];
                $products["mirsaProductVolume"] =       $row["mirsaProductVolume"];
                $products["mirsaProductCodeBType"] =   $row["mirsaProductCodeBType"];
                $products["mirsaProductCodeCType"] =   $row["mirsaProductCodeCType"];
                $productId = $row["mirsaProductId"];




$query2 = "SELECT mirsaProductUrlImage FROM mirsaProduct,mirsaProductImages
WHERE FK_mirsaProductId = '$productId' GROUP BY mirsaProductUrlImage";     

    $result2=mysqli_query($link, $query2);
    while ($row2 = mysqli_fetch_array($result2))
      {
             $image [] = $row2["mirsaProductUrlImage"];
             $products["mirsaProductUrlImage"]=$image;
      }
     array_push($response["Products"], $products);





        }
                  die(json_encode($response));


  }

// echoing JSON response

 else {
// no products found
$response["success"] = 0;
$response["message"] = "No Products";
   die(json_encode($response));
 }
    ?>

我收到了这样的回复

  {
 ProductId: "1",
 ProductCode: "118.023",
 ProductDescription: "NEM OVAL (Transparent)",
 ProductPcsInCartoon: "10",
 ProductWeight: "7.55 - 8.05(KG)",
 ProductVolume: "0.104(m3)",
 ProductCodeBType: "NA",
 ProductCodeCType: "NA",
 ProductUrlImage: [
  "1.png",
  "2.png"
 ]
 },


{
    ProductId: "2",
    ProductCode: "118.024",
    ProductDescription: "NEM OVAL (Opac)",
    ProductPcsInCartoon: "10",
    ProductWeight: "7.55 - 8.05(KG)",
    ProductVolume: "7.55 - 8.05(KG)",
    ProductCodeBType: "NA",
    ProductCodeCType: "NA",
    ProductUrlImage: [
            "1.png",
            "2.png"
          ]

第二个 JSON 对象不包含这 2 个图像 url,它采用 $image[] 的旧值,所以我需要重新初始化或清空他的值,我需要你们的帮助,你们永远是我们的支持

p>

【问题讨论】:

  • 您的代码容易受到SQL injection attacks 的攻击。您应该使用mysqliPDO 准备好的语句和this post 中描述的绑定参数。
  • 我知道它只是一个为我的 iOS 应用程序获取 json 值的 api 没有交易没有秘密信息......让我们专注于如何解决我的问题然后我会更新我的代码
  • 如果我理解正确的问题,你不需要 $image 数组。所以改为 -> $image [] = $row2["mirsaProductUrlImage"]; $products["mirsaProductUrlImage"]=$image;使用这个 -> $products["mirsaProductUrlImage"]=$row2["mirsaProductUrlImage"];
  • 不,伙计,我需要数组[],但每次他结束循环时我都需要清空数组

标签: php mysql


【解决方案1】:

我要试一试。由于您从未真正将 $image 初始化为数组。你真的打算:

$image[] = array()

成为:

$image = array();

这将导致 $image 数组在每次通过第一个 while 循环时都被重置。

【讨论】:

  • 仍然得到相同的响应
  • 请帮助大家
猜你喜欢
  • 1970-01-01
  • 2016-07-06
  • 2018-11-06
  • 1970-01-01
  • 2016-10-23
  • 1970-01-01
  • 2015-04-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多