$result = mysql_query("SELECT * FROM election WHERE election_date >= NOW() ORDER BY election_date LIMIT 1");
$result2 = mysql_query("SELECT has_voted from users where pid = 3");
$results = array();
$line1 = mysql_fetch_object($result2);
while($line = mysql_fetch_array($result, MYSQL_ASSOC)){
$results[] = $line;
}
array_push($results, $line1);
echo (json_encode($results));
That is my php code, what I want here is to output the two sql results as one. Below is the output that I will get when echoing the result.
[
{
election_id: "3",
election_title: "",
election_date: "2016/02/20",
start_time: "08:00",
end_time: "23:59",
election_venue: "",
status: "0",
num_needed: "3"
},
{
has_voted: "1"
}
]
As you can see, the has_voted: "1"
is separated from the first one. Is it possible for me to include that result to the first one? So the output that I want is this:
[
{
election_id: "3",
election_title: "",
election_date: "2016/02/20",
start_time: "08:00",
end_time: "23:59",
election_venue: "",
status: "0",
num_needed: "3"
has_voted: "1"
}
]
I have tried using the array_merge() and this is my code:
$result = mysql_query("SELECT * FROM election WHERE election_date >= NOW() ORDER BY election_date LIMIT 1"); $result2 = mysql_query("SELECT has_voted from users where pid = 3");
$results = array(); $results1 = array();
while($line = mysql_fetch_array($result, MYSQL_ASSOC)){ $results[] = $line; } while($line1 = mysql_fetch_array($result2, MYSQL_ASSOC)){ $results1[] = $line1;
}`
$final = array_merge($results, $results1); echo (json_encode($final));
The output is still not the one I wanted:
[ { election_id: "3", election_title: "", election_date: "2016/02/20", start_time: "08:00", end_time: "23:59", election_venue: "", status: "0", num_needed: "3" }, { has_voted: "1" } ]
Use array_merge() :
$finalArr = array_merge($results, $line1);
echo json_encode($finalArr);
We can use array_merge($line,$line1);
Then output will be
Array ( [election_id] => 3 [election_title] => [election_date] => 2016/02/20 [start_time] => 08:00 [end_time] => 23:59 [election_venue] => [status] => 0 [num_needed] => 3 [has_voted] => 1 )
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.