I seem to get this issue a few times and figure it out then the next time I come across it I can't figure it out.
So I have a SQL statement which returns to a $result variable as usual...
$result = $stmt->fetchAll( PDO::FETCH_ASSOC );
I will show you a slimmed down return below
Array
(
[eid] => 1
[eTitle] => premier league
[eDesc] =>
[cid] => 1
[deleted] => 0
[eStartTime] => 15:00
[eHome] => 1
[eAway] => 2
[eKickoff] => 15:00:00
[eDate] => 2016-08-20
)
Array
(
[eid] => 2
[eTitle] => fa cup
[eDesc] =>
[cid] => 1
[deleted] => 0
[eStartTime] => 15:00
[eHome] => 1
[eAway] => 2
[eKickoff] => 15:00:00
[eDate] => 2016-08-27
)
Now what i want to do is loop through the $result and on each key eHome and eAway run a function below is the pseudo code for what I am trying to do.
loop ( $reult )
$row[eHome][eHomeData] = $this->getTeam( $row['eHome'] )
$row[eHome][eAwayData] = $this->getTeam( $row['eAway'] )
So basically I want to rebuild the array with the home or away team data which uses the id given in eHome and eAway.
Should I do it like this or should I be using JOINS in MySQL?
Sorry if I have worded this badly I having been struggling for a few hours with this and gave up.
Thanks in advance guys.
Tom
This is what I have used in the end guys sorry for the poorly worded question. Is there a better way to do this, here is the code and then the print_r() dump for you to see where I was going with this.
$stmt = $this->conn->prepare('
SELECT * from events WHERE cid = :cid
');
$stmt->bindParam( ':cid', $categoryid, PDO::PARAM_INT );
if( $stmt->execute() )
{
$result = $stmt->fetchAll( PDO::FETCH_ASSOC );
$resultCount = count($result);
for( $i = 0; $i < $resultCount; $i++ ) {
$result[$i]['homeTeamData'] = $this->getTeam( $result[$i]['eHome'] );
$result[$i]['awayTeamData'] = $this->getTeam( $result[$i]['eAway'] );
}
}
Below is the print_r() which adds an extra awayTeamData and homeTeamData with the data collected in the loop added above
Array
(
[0] => Array
(
[eid] => 1
[eTitle] => premier league
[eDesc] =>
[cid] => 1
[deleted] => 0
[eStartTime] => 15:00
[eHome] => 1
[eAway] => 2
[eKickoff] => 15:00:00
[eDate] => 2016-08-20
[homeTeamData] => Array
(
[tid] => 1
[tName] => Chelsea
[deleted] => 0
)
[awayTeamData] => Array
(
[tid] => 2
[tName] => Man Utd
[deleted] => 0
)
)
[1] => Array
(
[eid] => 2
[eTitle] => fa cup
[eDesc] =>
[cid] => 1
[deleted] => 0
[eStartTime] => 15:00
[eHome] => 1
[eAway] => 2
[eKickoff] => 15:00:00
[eDate] => 2016-08-27
[homeTeamData] => Array
(
[tid] => 1
[tName] => Chelsea
[deleted] => 0
)
[awayTeamData] => Array
(
[tid] => 2
[tName] => Man Utd
[deleted] => 0
)
)
)
Ive sat with this for a while now, and its getting late.
Im trying to get a top 3 of most sales from last month, and i need to count how many times a id from array 1 is equal to array 2 last month(6 = last atm.) like id 4 = 2, id 7 = 3
It might not be the perfect solution, but im just trying to break it down by my self, so later on 'maybe' problems, will i take care of when i hit the wall,
so please, if anyone can help me here, ill be greatfull.
UPDATE
- I will add the result im looking for here: (sorry i didnt before, it makes it alot easier :-)
- The result below, is because i want the count from 2014-06-01 and up to the last day of that month monly, on array[0][1] under this array, only 6-7-8 is not from 2014-06
Hope it makes a bit more sense now ^^
Array
(
[0] => Array
(
[0] => Array
(
[4] => 2
[7] => 3
[1] => 2
[3] => 2
[9] => 1
[12] => 1
[2] => 1
[13] => 1
)
)
)
Array
(
[0] => Array
(
[0] => Array
(
[0] => 4
[1] => 4
[2] => 7
[3] => 1
[4] => 7
[5] => 7
[6] => 3
[7] => 3
[8] => 4
[9] => 9
[10] => 12
[11] => 2
[12] => 13
[13] => 1
)
[1] => Array
(
[0] => 2014-06-18
[1] => 2014-06-10
[2] => 2014-06-05
[3] => 2014-06-05
[4] => 2014-06-12
[5] => 2014-06-11
[6] => 2013-12-12
[7] => 2014-07-23
[8] => 2014-05-13
[9] => 2014-06-01
[10] => 2014-06-12
[11] => 2014-06-04
[12] => 2014-06-04
[13] => 2014-06-11
)
)
)
I hope that what I understood is what you're really asking for. let's say your array definition is :
$arr = Array
(
0 => Array
(
0 => Array
(
0 => 4,
1 => 4,
2 => 7,
3 => 1,
4 => 7,
5 => 7,
6 => 3,
7 => 3,
8 => 4,
9 => 9,
10 => 12,
11 => 2,
12 => 13,
13 => 1
),
1 => Array
(
0 => "2014-06-18",
1 => "2014-06-10",
2 => "2014-06-05",
3 => "2014-06-05",
4 => "2014-06-12",
5 => "2014-06-11",
6 => "2013-12-12",
7 => "2014-07-23",
8 => "2014-05-13",
9 => "2014-06-01",
10 => "2014-06-12",
11 => "2014-06-04",
12 => "2014-06-04",
13 => "2014-06-11"
)
)
);
If you need to test if the date is lower than 6 month and then put their id, sales and date you need to use this code
$result = [];
$index=0;
foreach ($arr[0][0] as $key => $value)
{
$date1 = new DateTime($arr[0][1][$key]);
$date2 = new DateTime();
$diff = $date1->diff($date2);
$diff = ($diff->format('%y') * 12) + $diff->format('%m');
if($diff<=6)
{
$result[$index]['id'] = $key;
$result[$index]['sales'] = $value;
$result[$index]['date'] = $arr[0][1][$key];
$index++;
}
}
var_dump($result);
array_count_values() will give you the number of times each value appears in array 1.
$count = array_count_values($array[0][0]);
Result:
Array
(
[4] => 3
[7] => 3
[1] => 2
[3] => 2
[9] => 1
[12] => 1
[2] => 1
[13] => 1
)
Then you can use a loop to combine with array 2:
$result = array();
foreach($count as $key=>$val) {
$result[$array[0][1][$key]] = $val;
}
Result:
Array
(
[2014-06-12] => 3
[2014-07-23] => 3
[2014-06-10] => 2
[2014-06-05] => 1
[2014-06-01] => 1
[2014-06-04] => 1
[2014-06-11] => 1
)
See demo
$merged = array();
while($row = mysqli_fetch_assoc($get_data_res)){
$merged[] = $row;
}
echo '<pre>'; print_r($merged); echo '</pre>';
After Printing Array I am getting this output
Array
(
[0] => Array
(
[lees_than_60] => 50
[one_four] => 48
[four_min] => 0
[id] => 24
[first_name] => Yogini
[log_in_time] => 0000-00-00 00:00:00
[log_out_time] => 0000-00-00 00:00:00
[calldate] => 2014-05-22
[clid] => 2433
)
[1] => Array
(
[lees_than_60] => 14
[one_four] => 6
[four_min] => 2
[id] => 28
[first_name] => Vijay
[log_in_time] => 2014-05-28 16:57:51
[log_out_time] => 2014-05-28 16:58:12
[calldate] => 2014-05-22
[clid] => 2436
)
[2] => Array
(
[lees_than_60] => 64
[one_four] => 60
[four_min] => 0
[id] => 23
[first_name] => Pratibha
[log_in_time] => 2014-05-28 15:55:18
[log_out_time] => 2014-05-28 15:55:44
[calldate] => 2014-05-22
[clid] => 2431
)
)
My question is I want to create below array like I want to create new array against same calldate 2014-05-22 ie only one calldate and remaining all data like:
[calldate] => 2014-05-22
show whole data of aove call date should not repeat by merging
I have an array of something like this:
Array ( [0] => stdClass Object ( [id] => 41 [title] => test2 [alias] => test2 [catid] => 8 [published] => 1 [introtext] =>
test2
[fulltext] => [video] => [gallery] => [extra_fields] => [] [extra_fields_search] => [created] => 2012-08-27 16:37:51 [created_by] => 62 [created_by_alias] => [checked_out] => 0 [checked_out_time] => 0000-00-00 00:00:00 [modified] => 0000-00-00 00:00:00 [modified_by] => 0 [publish_up] => 2012-08-27 16:37:51 [publish_down] => 0000-00-00 00:00:00 [trash] => 0 [access] => 1 [ordering] => 15 [featured] => 0 [featured_ordering] => 0 [image_caption] => [image_credits] => [video_caption] => [video_credits] => [hits] => 0 [params] => JParameter Object ( [_raw:protected] =>
and etc in that array ( it has alot of things in it ).
Now it is displaying like this
Item | date
Item | date
Item | date
What I want to do is take that array and sort it by aggregate date
Somethink like this
Aggr date
Item | date
Item | date
Aggr date
Item | date
Item | date
Is this even possible given this array ?
Is this what you are looking for?
$newArray = array();
foreach ($myArrayOfStdClasses as $key => $obj) {
$newArray[date('Y-m-d', strtotime($obj->created]))[] = array('title' => $obj->tile, 'date' => $obj->created);
}
You can use usort to define your sorting function:
usort($items, function($item1, $item2){
if($item1->created == $item2->created)
return 0;
return $item1->created < $item2->created ? -1 : 1;
});
This will just sort them. Then on output as you loop through them you can do the aggregation based on day, hour, month however...
In the multidimensional array below, I would like to merge arrays that have the same merge_id. I'm not sure "merge" is the right word: in the example below, array['0'] should become array['0'] with in it array['0']['0'] and array['0']['1'], the latter being equal to array['1']. I hope this makes sense ...
The array comes out of the db sorted on merge_id so arrays with matching merge_id are always "next to" each other, and there will only ever be 2 with the same merge_id
As I loop through the array I know I need to keep a variable that is always equal to the previous merge_id and if there is a match between previous and current, then merge.
Array
(
[0] => Array
(
[client_id] => 5
[company_name] => company111_name
[id] => 3
[fee] => 111
[year] => 2009
[quarter] => 3
[date_inserted] => 1264948583
[description] => 2009 - Q3
[fee_type] =>
[merge_id] => a87ff679a2f3e71d9181a67b7542122c
[total_paid] => 0
[total_remainder] => 0
)
[1] => Array
(
[client_id] => 5
[company_name] => company111_name
[id] => 6
[fee] => 55.5
[year] => 2010
[quarter] => 2
[date_inserted] => 1264949470
[description] => 2010 - Q2
[fee_type] =>
[merge_id] => a87ff679a2f3e71d9181a67b7542122c
[total_paid] => 0
[total_remainder] => 0
)
[2] => Array
(
[client_id] => 5
[company_name] => company111_name
[id] => 4
[fee] => 111
[year] => 2009
[quarter] => 4
[date_inserted] => 1264948583
[description] => 2009 - Q4
[fee_type] =>
[merge_id] =>
[total_paid] => 0
[total_remainder] => 0
)
[3] => Array
(
[client_id] => 5
[company_name] => company111_name
[id] => 7
[fee] => 55.5
[year] => 2010
[quarter] => 3
[date_inserted] => 1264949470
[description] => 2010 - Q3
[fee_type] =>
[merge_id] =>
[total_paid] => 0
[total_remainder] => 0
)
)
Code
$merger = $data['search']['0']['merge_id'];
$i = 0;
foreach($data['search'] as $fee)
{
if($fee['merge_id'] == $merger)
{
//bump up & merge arrays
???
}
$merger = $fee['merge_id'];
$i++;
}
You can use the ID as key to put all items with the same ID in the same array:
$merged = array();
foreach ($data['search'] as $fee) {
if ($fee['merge_id'] == '') {
continue;
}
if (!isset($merged[$fee['merge_id']])) {
$merged[$fee['merge_id']] = array();
}
$merged[$fee['merge_id']][] = $fee;
}
$merged = array_values($merged);
Notice that this will skip the items with an empty merge ID. You could also use a default merge ID in that case by replacing continue; with $fee['merge_id'] = 0;.
foreach($array as $p)
$result[$p['merge_id']][] = $p;