如何在php中用数组打印方括号内的括号

时间:2017-10-26 09:29:52

标签: php arrays

我正在回显我的数组,但它提供了不需要的输出,我需要我的输出有括号,其中包含数组方括号内的数据。 请帮我解决这个问题

所需输出{"error":false,"user":[{"id":1,"name":"Maurice Obraenne","mobile":"0785984505","sutdentId":"Muberangango Joseph"}]}

我得到的不良输出是以下方括号内没有括号,并且没有记录的识别名称: {"error":false,"user":[1,"Maurice Obraenne","0785984505","Muberangango Joseph"]}

我的代码:

       $stmt->bind_param("i", $mobile);
    if ($stmt->execute()) {

   $result = $stmt->get_result();

    $usersArr = array();
    while ($user = $result->fetch_assoc()){

    $usersArr[] = $user['id'];
    $usersArr[] = $user['name'];
    $usersArr[] = $user['mobile'];
    $usersArr[] = $user['sutdentId'];
    $usersArr[] = $user['user_id'];
    $usersArr[] = $user['full_name'];
    $usersArr[] = $user['school'];
    $usersArr[] = $user['level'];
    $usersArr[] = $user['year'];
    $usersArr[] = base64_encode($user['photo']);   
    $usersArr[] = $user['busId'];
    $usersArr[] = $user['plate_no'];
    $usersArr[] = $user['DriverId'];
    $usersArr[] = $user['driverphone_number'];
    $usersArr[] = $user['driver_fullname'];

     }
   $stmt->close();
   // echo json_encode($usersArr);
    return $usersArr;

  } else {
return NULL;
  }

3 个答案:

答案 0 :(得分:1)

你可以这样做:

var program = null;

function initDemo() {
    program = Program.getProgram();
    program.init();
    runDemo();
};

function runDemo() {
    if(program != null && program.run) {
        program.execute();
    }
    requestAnimationFrame(runDemo);
};

答案 1 :(得分:0)

你只是让你的生活更加艰难:

  $stmt->bind_param("i", $mobile);
  if ($stmt->execute()) {    
       $result = $stmt->get_result();
       $usersArr = $result->fetch_all(MYSQLI_ASSOC);
       $stmt->close();
       $result->free(); //Because why not?
       return array_map(function ($user) {
             $user['photo'] = base64_encode($user['photo']);
             return $user;
      }, $usersArr;);
  } else {
       return NULL; 
  }

答案 2 :(得分:0)

您希望输出显示每个用户都在一个数组中,因此您必须将每个用户存储在一个数组中。

使用此代码:

while ($user = $result->fetch_assoc()){

    $temp = array();
    $temp = $user;
    $temp['photo'] = base64_encode($user['photo']); 
    $usersArr[] = $temp;


}

然后您可以使用此数组将用户数据存储在数组的user

像这样:

$final['error'] = '';
$final['user'] = $usersArr;