我试图创建一个多维关联数组但是当我对数组进行json编码时,它会将其显示为Dict而不是数组。这可能是我使用钥匙的方式 $ this-> jsonArray ['朋友'] [$ status] = $ friend;
<?php
// Connection to Database
include('connection.php');
// Username Submitted
$username = $_POST['signinusername'];
// Password Submitted
$password = $_POST['signinpassword'];
// Creates new instance of Login Class
$user = new Login($username, $password, $db);
/**
* Login Class
*
* This class takes the username and password posted and checks whether they match
* a username and password in the database if they do two other functions are called
* that get both the friends of that user and the groups they are in. All data is encoded in json
*
* @copyright 2014 Phil and Matt Technologies
* @license http://www.php.net/license/3_01.txt PHP License 3.0
* @version Release: @package_version@
* @link http://grouporder.site90.net/signin.php
* @since Class available since Version 1.0
*/
class Login
{
private $connection;
private $id;
private $username;
private $password;
private $status;
private $idArray = array();
private $nameArray = array();
private $jsonArray = array('Friends' => array(),'Groups' => array());
/**
* Function Constructor
*
* @param Name $username Stores the submitted username in a private variable.
* @param Password $password Stores the submitted password in a private variable.
* @param Object $db Creates new mysqli connection to database and stores it in a private variable.
*/
function __construct($username, $password, $db)
{
$this->username = $username;
$this->password = $password;
$this->result_array;
$this->connection = $db;
$this->authenticate();
}
/**
* Checks if username and password are correct
*
*
* @return Status
*
*/
private function authenticate()
{
$query = "SELECT idUser,Password FROM Users
WHERE Username = ?
LIMIT 1 ";
if($stmt = $this->connection->prepare($query))
{
$stmt->bind_param('s', $this->username);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($this->id,$hash);
$stmt->fetch();
// compare the password to the expected hash
if (password_verify($this->password, $hash))
{
$this->status = 'Sucess';
$this->getFriends();
$this->Groups();
}
else
{
$this->status = 'Failed';
}
}
$stmt->close();
$this->jsonArray['Status'] = $this->status;
}
/**
* Checks database for friends of logged in user.
*
*
* @return Friends List
*
*/
private function getFriends()
{
$query = "SELECT Username, Status
FROM Users
INNER JOIN Friends
ON Users.idUser = Friends.idFriend
WHERE Status = 'Accepted' AND Friends.idUser = ? ";
// Prepares and excutes the query
if($stmt = $this->connection->prepare($query))
{
$stmt->bind_param('s', $this->id);
$stmt->execute();
$stmt->bind_result($friend, $status);
for ($i=0; $stmt->fetch(); $i++)
{
$this->jsonArray['Friends'][$status] = $friend;
}
}
}
/**
* Checks database for groups and members of that group of logged in user.
*
*
* @return Group List
*
*/
private function Groups()
{
// Mysql query that gets all group names in the database that the user is a member of
$groupName = "SELECT GroupName, Groups.idGroup
FROM Groups
INNER JOIN Members
ON Groups.idGroup = Members.idGroup
WHERE Members.idMember = ? ";
// Mysql query that gets all members in each group that the user is a member of
$groupMembers = "SELECT Users.Username, Users.idUser
FROM Members
INNER JOIN Users
ON Members.idMember = Users.idUser
WHERE idGroup = ? ";
// Prepares and excutes the query
if($stmt = $this->connection->prepare($groupName))
{
$stmt->bind_param('s', $this->id);
$stmt->execute();
$stmt->bind_result($groupName, $idGroup);
$stmt->store_result();
$groupNum = $stmt->num_rows;
while ($stmt->fetch())
{
$this->idArray[] = $idGroup;
$this->nameArray[] = $groupName;
}
}
$stmt->close();
// For loop that checks the database for members part of users groups and appends them to array grouplist
for ($i=0; $i < $groupNum; $i++)
{
// Helper Variable that holds each helperArray element
$idVariable = $this->idArray[$i];
$nameVariable = $this->nameArray[$i];
$this->jsonArray['Groups'][$nameVariable]['GroupID'] = $idVariable;
// Prepares and excutes the query
if($stmt = $this->connection->prepare($groupMembers))
{
$stmt->bind_param('s', $idVariable);
$stmt->execute();
$stmt->bind_result($username, $userid);
for ($j = 0; $stmt->fetch(); $j++)
{
// Adds user to list of users of that group
$this->jsonArray['Groups'][$nameVariable]['GroupMembers'][$userid] = $username;
}
}
}
}
/**
* Function Destructor
*
*/
public function __destruct()
{
echo json_encode($this->jsonArray);
}
}
?>
Json输出
{"Friends":{"Accepted":"test2"},"Groups":{"Group1":{"GroupID":1,"GroupMembers":
{"1":"test"}}},"Status":"Sucess"}