dongnanbi4942 2014-07-28 12:25
浏览 41
已采纳

将变量循环到Array中,然后将其编码为1 JSON

I had been searching for a while, and I can't seem to find a solution for my problem, so I created an account.

My situation is as below.

  1. I am doing a for loop upload image form.

  2. Every time it loops, it will have 1 $Listingid.photo

  3. I would like to add all the $Listingid.photo into an array, then package it into 1 JSON. and echo it.

And I am not very sure how to approach it. At the moment it is creating 1 JSON response per loop.

Only the last two lines of my code really matters to the problem.

My code:

<?php
ini_set('display_errors', 1); error_reporting(E_ALL);

ob_start();
session_start();
include 'connect.php';

$Listingid =($_POST['listingid']);

if(isset($_FILES['file'])) {
    $_FILES["file"]["name"] = preg_replace('/\s+/', '', $_FILES["file"]["name"]);
    for($i=0;$i<count($_FILES["file"]["name"]);$i++)
    {

        $photo=$_FILES["file"]["name"][$i];
        $newname=$Listingid.$photo;
        move_uploaded_file($_FILES["file"]["tmp_name"][$i], "photo/$Listingid$photo");

        mysqli_query($con,"INSERT INTO listingpic (pic,listingid) VALUES ('$newname','$Listingid');") or die(mysqli_error($con));

        $arr = array('picname' => $Listingid.$photo);
        echo json_encode($arr);
    }
}
  • 写回答

1条回答 默认 最新

  • douju1997 2014-07-28 12:29
    关注
    $arr[]=array('picname' => $Listingid.$photo);
    

    then move the echo json_encode out of the loop

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?