I have this query for mysql:
SELECT HOUR(time),COUNT(*) FROM pageview WHERE time >= DATE_SUB(NOW(),INTERVAL 12 HOUR) GROUP BY HOUR(time)
For example, this is the output:
Array
(
[0] => Array
(
[HOUR(time)] => 1
[COUNT(*)] => 1
)
[1] => Array
(
[HOUR(time)] => 10
[COUNT(*)] => 4
)
[2] => Array
(
[HOUR(time)] => 11
[COUNT(*)] => 5
)
)
However I want the output like this
Array
(
[1] => 1
[10] => 4
[11] => 5
)
The array index should be the value of [HOUR(time)].
I would prefer directly by changing the query.
To fetch the data I use this:
$stmt = $db->prepare($query);
$result = $stmt->execute();
$views = $stmt->fetchAll();
indexes in mysql results create automatically , to achieve your goal loop through your first array and create new one as you want
$result = array();
foreach($views as $row) {
$result[$row['HOUR(time)']] = array( 'COUNT(*)' => $row['COUNT(*)']);
}
print_r($result); // check output
or simpler form
$result = array();
foreach($views as $row) {
$result[$row['HOUR(time)']] = $row['COUNT(*)'];
}
print_r($result); // check output
Try with this
$res = array();
foreach($views as $data)
{
$res[$data['HOUR(time)']] = array( 'COUNT(*)' => $data['COUNT(*)']);
}
print_r($res);
Related
I made a function:
function xyz( $user_id_array = NULL ){
if ( ! $user_id_array ) {
$result = mysqli_query($con, "SELECT DISTINCT(benutzer_id) FROM anstellung;");
$user_id_array = mysqli_fetch_all($result);
}
print("<pre>".print_r($user_id_array,true)."</pre>");
}
Now my question is if I call the function like this:
xyz( array(6, 2, 3) );
xyz( );
The output looks like this:
Array
(
[0] => 6
[1] => 2
[2] => 3
)
Array
(
[0] => Array
(
[0] => 9
)
[1] => Array
(
[0] => 13
)
)
How is it possible to have the same dimensions of the arrays?
The mysqli_fetch_all should look like this as well:
Array
(
[0] => 9
[1] => 13
)
You can use mysqli_fetch_assoc() with a loop to construct the desired array:
if (!$user_id_array) {
$user_id_array = Array();
$result = mysqli_query($con, "SELECT DISTINCT(benutzer_id) FROM anstellung;");
while ($row = mysqli_fetch_assoc($result)) {
$user_id_array[] = $row['benutzer_id'];
}
}
I have following query in loop for select records
$shopchairs=$rows['shop_chairs']; //coming dynamically i.e. 1 or 3 or 4
$result = [];
for($i=1;$i<=$shopchairs;$i++)
{
$query = //SELECT QUERY WITH WHERE CONDITION;
$res = $query->result_array();
$result[] = [
'chairnumber' => $i,
'status' => $querys->num_rows() > 0 ? 'booked' : 'available',
'estimateTime' => "",
];
}
echo "<pre>";print_R($result);
Now I just want to merge "estimateTime" which is coming from $res(array) with $result(array), How can I do this?
Right now my $res showing following array in the loop
Array
(
[0] => Array
(
[shopId] => 2
[duration] => 20
[estimatedTime] => 11.3000
)
)
Array
(
)
Array
(
)
Array
(
)
I just want to merge estimatedTime with $result array
I need to make my array better.
I am getting data from database and i have milestones and milestone_parts. i want two-dimensional array. I need data of milestones in the first dimension and milestone_parts in the second dimension.
With this code:
$query = "
SELECT
a.id AS `milestone_id`,
a.titel AS `milestone_titel`,
a.client AS `client`,
a.verkocht_id AS `milestone_verkocht_id`,
b.id AS `milestonefase_id`,
b.titel AS `milestonefase_titel`,
b.milestone_id AS `milestonefase_milestone_id`,
b.omschrijving AS `milestonefase_omschrijving`
FROM `milestones` a
INNER JOIN `milestone_parts` b ON a.id=b.milestone_id
WHERE a.verkocht_id = '99'
";
$result= $db->query($dbh, $query);
while ($row = $db->fetchassoc($result))
{
$stone = array($row['milestone_verkocht_id'], $row['milestone_id'], $row['milestone_titel'], $row['client']);
$fase = array($row['milestonefase_milestone_id'],$row['milestonefase_id'],$row['milestonefase_titel']);
$stone[] = $fase;
echo '<pre>'; print_r($stone); echo '</pre>';
}
I get this as result
Array
(
[0] => 99
[1] => 6
[2] => string
[3] => string
[4] => Array
(
[0] => 6
[1] => 10
[2] => string
)
)
Array
(
[0] => 99
[1] => 6
[2] => string
[3] => string
[4] => Array
(
[0] => 6
[1] => 11
[2] => string
)
)
but I need (with names) this:
Array
(
[milestone_verkocht_id] => 99 // This is project id
[milestone_id] => 6
[milestone_title] => string
[client] => string
[10] => Array
(
[milestonefase_milestone_id] => 6
[milestonefase_id] => 10
[milestone_title] => string
)
[11] => Array
(
[milestonefase_milestone_id] => 6
[milestonefase_id] => 11
[milestone_title] => string
)
[12] => Array
(
[milestonefase_milestone_id] => 6
[milestonefase_id] => 12
[milestone_title] => string
)
)
Can you help me or do you have a solution? Help me please!
you can cycle each field returned by the query, checking the field name and making new arrays
$stones = array();
while ($row = $db->fetchassoc($result)) {
$fase = array();
$stone = array('milestones' => array());
foreach ($row as $k => $v) {
if (strpos($k, 'milestonefase_') === 0) {
$fase[$k] = $v;
} else {
$stone[$k] = $v;
}
}
if(!isset($stones[$stone['milestone_id']])) {
$stones[$stone['milestone_id']] = $stone;
}
$stones[$stone['milestone_id']]['milestones'][$fase['milestonefase_id']] = $fase;
}
echo '<pre>'.print_r($stones, true).'</pre>';
Edit
made some changes in order to match the request. Now we use $stones to store the information we already have on a milestone, adding to it the different "$fase" returned from the query
Probably a more clean way is to retrieve all the information with two different queries one for milestones and the other for the fases
Edit2
Added a sub-array for the milestone fases
I have a php /mysqli query and I want to populate an array with the results:
$query3 ="SELECT * FROM conditions";
$results = array();
if ($result = mysqli_query($conn, $query3)){
while($row = mysqli_fetch_assoc($result))
{
$results[] = $row;
}
}
print_r($results);
Something is wrong here -its making arrays within arrays I think. (to be honest I am confused by this result)
How do I do this correctly!
Array (
[0] => Array ( [condition_id] => 1 [condition_name] => Epilepsy )
[1] => Array ( [condition_id] => 2 [condition_name] => ASD )
[2] => Array ( [condition_id] => 3 [condition_name] => BESD )
[3] => Array ( [condition_id] => 4 [condition_name] => HI )
[4] => Array ( [condition_id] => 5 [condition_name] => Medical )
[5] => Array ( ...
Thanks for all the help - now how should I create what I actually want which is one array with key=>value like this:
array (1=>epilepsy, 2=>ASd...) - the numbers refer to the primary key.
How do I populate an array from this query please?
Change your code as below :
while($row = mysqli_fetch_assoc($result))
{
$results[$row['condition_id']] = $row['condition_name'];
}
Move to PDO, Luke.
$results = $pdo->query("SELECT FROM conditions")->fetchAll(PDO::FETCH_KEY_PAIR);
print_r($results);
Whoops! Is that all the code?
I have a particular mysqli query that returns an empty element at the 0th position in the result set array. This empty non-object element seems to cause problems when I try to loop through the result set to display to screen
$projAreas[] = array();
$projectID = $_GET['projectID'];
$sql = "SELECT *
FROM `areas` , `project_area_junc`
WHERE `areas`.`areaID` = `project_area_junc`.`areaID`
AND `project_area_junc`.`projectID` = $projectID";
$results = $conn->query($sql);
while($row = $results->fetch_object()) {
$projAreas[] = $row;
}
The result gives unwanted array element at position 0)
i.e. when I print_r($projAreas)
I get this:
Array
(
[0] => Array
(
)
[1] => stdClass Object
(
[areaID] => 56
[propertyID] => 14
[areaName] => Living Room
[areaInfo] => lots of windows - colonial style
[proj_area_juncID] => 10
[projectID] => 4
)
[2] => stdClass Object
(
[areaID] => 57
[propertyID] => 14
[areaName] => Kitchen
[areaInfo] =>
[proj_area_juncID] => 11
[projectID] => 4
)
Try this ....
$projAreas = array();
while($row = $results->fetch_object()) {
array_push($projAreas ,$row);
}
print_r($projAreas);
try this
$projAreas= array();
instead of
$projAreas[]= array();
you have declare the array $projAreas as well initialize it by [] which takes first index 0