php merging arrays - php

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;

Related

Get Data from database using array values with for loop in PHP

Array:
$ecodesAr = Array (
[0] => 1Z0-060
[1] => 98-375
[2] => 98-368
[3] => 98-367 )
for($k=0; $k<count($ecodesAr); $k++){
$arrayTB[$k] = $this->Functions->exam('title', $ecodesAr[$k]); }
Modal code:
public function exam($q, $d) {
$q = $this->db->where($q, $d)
->get('exams');
return $q->row();
}
Result:
Array (
[0] =>
[1] =>
[2] =>
[3] => stdClass Object ( [id] => 1091 [hot_exam] => 0 [top_exam] => 0 [category] => 114 [subcats] => 288 [slug] => 98-367 [sku] => OI5Ao [title] => 98-367 [name] => MTA Security Fundamentals [update] => 2021-09-16 [regular_price] => 130 [sale_price] => 59 [on_homepage] => 0 [on_request] => 0 [expired] => 0 [path_slug] => 98-367.pdf [questions] => 123 [demo_slug] => 98-367-demo.pdf [prc_price] => 65 [prc_demo] => 98-367-demo [prc_exam] => 98-367 [is_active] => 1 )
)
The First 3 values are skiped in the output and just the last value got, I want to all array data against values please help anyone!
$productsarry = Array (
[0] => Milk
[1] => Cream
[2] => Sugar
[3] => Yougert
);
for($k = 0; $k < count($productsarry); $k++) {
$arrayTB[$k] = query("select slug, qty, name, price from exams where title ='$ecode'")->row();
}

search inside arrays with condition in php

I have an api with a list of arrays inside one array and each array inside this array it has one key [attributes] and inside this key one array with key [CITY_NAME]
this is my array coming from api
this array number is 0 but every day this number is changed and my figures come wrong, what is the good way to always have my city figures using the key [CITY_NAME]
I am fetching the data using this code
$json = file_get_contents($url);
$json_data = json_decode($json, true);
$data = $json_data['features'];
$mycity = $data[0]['attributes'];
Array
(
[0] => Array
(
[attributes] => Array
(
[CITY_NAME] => city1
[Name] => city1
[ADMIN_NAME] => city1
[POP_CLASS] => 5,000,000 to10,000,000
[Population] => 7676654
[Population_Data_Source] => Wikipedia
[Population_Data_Date] => 2018
[CityID] => 14
[Longitude] => 46.7614868685786
[Latitude] => 24.7388786516234
[Confirmed] => 0
[Recovered] => 0
[Deaths] => 0
[Active] => 0
[Tested] => 0
[Name_Eng] => city1
[Join_Count] => 61
[Confirmed_SUM] => 5152
[Deaths_SUM] => 9
[Recovered_SUM] => 1407
[Active_SUM] => 3736
[Tested_SUM] => 376607
[ObjectId] => 14
)
)
[1] => Array
(
[attributes] => Array
(
[CITY_NAME] => city2
[Name] => city2
[ADMIN_NAME] => city2
[POP_CLASS] => 1,000,000 to 5,000,000
[Population] => 1675368
[Population_Data_Source] => Wikipedia
[Population_Data_Date] => 2010
[CityID] => 9
[Longitude] => 39.8148987363852
[Latitude] => 21.4273876500039
[Confirmed] => 0
[Recovered] => 0
[Deaths] => 0
[Active] => 0
[Tested] => 0
[Name_Eng] => city2
[Join_Count] => 59
[Confirmed_SUM] => 6848
[Deaths_SUM] => 85
[Recovered_SUM] => 1145
[Active_SUM] => 5618
[Tested_SUM] => 0
[ObjectId] => 9
)
)
Since I may have misunderstood and you may have many, just build a new array with CITY_NAME:
foreach($data as $values) {
$result[$values['attributes']['CITY_NAME']] = $values['attributes'];
}
Now you can access by CITY_NAME:
echo $result['city1']['Population'];
This might be easier. Extract all attributes from all arrays into an array, then create an array from that indexed by CITY_NAME:
$data = array_column(array_column($json_data['features'], 'attributes'),
null, 'CITY_NAME');

2 arrays count how many times id = date

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

PHP Sort Multi-Tired ARRAY by multiple sorts?

i have an php array of sports scores and i want to sort by the following
Wins Desc, Ties Desc, Points Scored Desc, Points Allowed Asc
anyone know how i can do this?
This array was generated by multiple mysql quires
[14] => Array
(
[title] => Crushers
[team_color] => #0000ff
[wins] => 0
[losses] => 1
[ties] => 1
[pointsscored] => 18
[pointsallowed] => 19
)
[15] => Array
(
[title] => Purple Stars
[team_color] => #ae25d9
[wins] => 1
[losses] => 1
[ties] => 0
[pointsscored] => 20
[pointsallowed] => 21
)
[16] => Array
(
[title] => Lighting Boltz
[team_color] => #d6c128
[wins] => 0
[losses] => 0
[ties] => 2
[pointsscored] => 12
[pointsallowed] => 12
)
[17] => Array
(
[title] => The Black Cheetahs
[team_color] => #000000
[wins] => 1
[losses] => 0
[ties] => 1
[pointsscored] => 15
[pointsallowed] => 13
)
I made a function to sort every multidimensional array by one of his columns ,look at array_multisort() and customize this function to your need:
function sortArrayBy($array , $column_name,$sort=SORT_DESC){
foreach ($array as $key => $row) {
$column[$key] = $row[$column_name];
}
array_multisort($column, $sort, $array);
return $array;
}
Call it like this :
<?php sortArrayBy($yourArray,'date') ; ?>

Sort an Array of Objects within Array by value

Basically I'm trying to sort a complex array of Objects within an array:
Array
(
[190515] => stdClass Object
(
[nid] => 15740686
[venue_nid] => 190515
[occurrences] => 1
[this_weeks_occurrences] => 0
[end_date] => 1350853200
[end_date_end_time] => 1350853200
[is_ongoing] => 0
[title] => Wentz Concert Hall and Fine Arts Center
[times] => Array
(
[0] => stdClass Object
(
[nid] => 15740686
[venue_nid] => 190515
[venue_title] => Wentz Concert Hall and Fine Arts Center
[datepart] => 20121021
[occurrences] => 1
[times] => Sun 4:00pm
[end_times] => Sun 4:00pm
[next_year] => 0
[next_month] => 0
[next_week] => 3
[occurrence_date] => 1350853200
)
)
[times_list] => Array
(
[0] => Oct 21 sun 4:00pm
)
)
[31403] => stdClass Object
(
[nid] => 15740686
[venue_nid] => 31403
[occurrences] => 1
[this_weeks_occurrences] => 0
[end_date] => 1350176400
[end_date_end_time] => 1350176400
[is_ongoing] => 0
[title] => KAM Isaiah Israel
[times] => Array
(
[0] => stdClass Object
(
[nid] => 15740686
[venue_nid] => 31403
[venue_title] => KAM Isaiah Israel
[datepart] => 20121014
[occurrences] => 1
[times] => Sat 8:00pm
[end_times] => Sat 8:00pm
[next_year] => 0
[next_month] => 0
[next_week] => 2
[occurrence_date] => 1350176400
)
)
[times_list] => Array
(
[0] => Oct 13 sat 8:00pm
)
)
[33861] => stdClass Object
(
[nid] => 15740686
[venue_nid] => 33861
[occurrences] => 1
[this_weeks_occurrences] => 0
[end_date] => 1350781200
[end_date_end_time] => 1350781200
[is_ongoing] => 0
[title] => Music Institute of Chicago, Nichols Concert Hall
[times] => Array
(
[0] => stdClass Object
(
[nid] => 15740686
[venue_nid] => 33861
[venue_title] => Music Institute of Chicago, Nichols Concert Hall
[datepart] => 20121021
[occurrences] => 1
[times] => Sat 8:00pm
[end_times] => Sat 8:00pm
[next_year] => 0
[next_month] => 0
[next_week] => 3
[occurrence_date] => 1350781200
)
)
[times_list] => Array
(
[0] => Oct 20 sat 8:00pm
)
)
)
I need to sort by occurrence_date and ensure that the data in the top Object (e.g.190515) doesn't become corrupted with other objects and to include the possibility that there could be a 2nd occurrence_date for the "times" [array] of Objects.
I've seen similar sort arrays of objects by field here, but not with the depth of the array/object. I tried using usort but I don't think my syntax to the value is correct.
Basically what I tried.
function cmp($x, $y) {
if ($x->occurrence_date > $y->occurrence_date) {
return 1; }
else {
return -1; }
}
usort($node->timeout_events_schedule->venues, 'cmp');
Thanks in advance
How about:
function compare($x,$y)
{
if($x->times[0]->occurrence_date == $y->times[0]->occurrence_date)
return 0;
elseif($x->times[0]->occurrence_date < $y->times[0]->occurrence_date)
return -1;
else
return 1;
}
uasort($your_array,'compare');
uasort() preserve your keys, unlike usort()
http://www.php.net/manual/en/function.uasort.php
Few tips to help you solve your problem:
you will need uasort function, to preserve associative keys
You can not access occurrence_date key directly from $x o r $y, you will need $x->times[0]->occurence_date
Before doing comparison, iterate through $x->times just to check if there are more entries, pick one that suits your needs.
since occurence_date is a number you don't have to use string comparison function
Good luck :)

Categories