-1

I have this query (it is working):

 $filesQuery = "SELECT COUNT( cs_file.iduser ) AS newFile, DATE( cs_file.add_date ) AS
            dateAdded
            FROM cs_file
            INNER JOIN cs_team_has_user ON cs_team_has_user.iduser = cs_file.iduser
            INNER JOIN cs_portal_has_team ON cs_portal_has_team.idteam = cs_team_has_user.idteam
            WHERE cs_file.add_date  >= DATE(NOW()) - INTERVAL {$range} DAY
            AND cs_file.isImage = 0
            GROUP BY DATE( cs_file.add_date )
            WITH rollup";

The query gives me this result:

[0]=>
  array(2) {
    ["newFile"]=>
    string(2) "12"
    ["dateAdded"]=>
    string(10) "2016-03-23"
  }
  [1]=>
  array(2) {
    ["newFile"]=>
    string(1) "2"
    ["dateAdded"]=>
    string(10) "2016-04-01"
  }
  [2]=>
  array(2) {
    ["newFile"]=>
    string(2) "10"
    ["dateAdded"]=>
    string(10) "2016-04-22"
  }....

What I need to make is make dateAdded a key and newFile (the count) a value. Like this:

[
'2016-03-23' => '12',
'2016-04-01' => '2',
'2016-04-22' => '10'
]

Is this possible with the query? Or there is some more efficient way, then using foreach loop?

Sasha
  • 8,521
  • 23
  • 91
  • 174
  • 1
    It can be done easily using [array_column()](http://www.php.net/manual/en/function.array-column.php): `$result = array_column($result, 'newFile', 'dateAdded');` – Mark Baker Sep 06 '16 at 08:51

5 Answers5

1

No, i don't think it can be done with Mysql query. You can use foreach for this:

$output = array();
foreach($output as $val){
    $output[ $val['dateAdded'] ] = $val['newFile'];
}
print_r($output);
d.coder
  • 1,988
  • 16
  • 23
1

You can use array_map with closure:

<?php

$r = array(
    array('idx'=>22, 'val'=>55),
    array('idx'=>33, 'val'=>77),
    array('idx'=>44, 'val'=>44),
);

$result = array();

array_map(function($val) use (&$result) {
    $result[$val['idx']] = $val['val'];
}, $r);


print_r($result);
aslawin
  • 1,981
  • 16
  • 22
1

don't need foreach just do it with array_column

array_column — Return the values from a single column in the input array.

array_column() returns the values from a single column of the input, identified by the column_key. Optionally, an index_key may be provided to index the values in the returned array by the values from the index_key column of the input array.

$final_array = array_column($result, 'newFile', 'dateAdded');
JYoThI
  • 11,977
  • 1
  • 11
  • 26
0

You need a foreach loop.

$res = [];
foreach($array as $value){
  $res[$value['dateAdded']] = $value['newFile'];
}
Ravi Hirani
  • 6,511
  • 1
  • 27
  • 42
0

Try the following code, this uses foreach loop since i don't think a query will be enough, but i may be wrong.

$myarray = array();
foreach($yourarray as $key => $value)
{
  $myarray[$value['dateAdded']] = $value['newfile'];
}
print_r($myarray); //tocheack

It works with me. You could remove the $key and just use value.

What this does is take an empty array, give it a custom key from your old array, it uses the name since it is an associative array. then from there you can assign a value using = operator and use the associative array again

you can ask me for more help if you need clarification :D

Goodluck meyt

Lorenzo
  • 574
  • 5
  • 13