Reputation: 293
I'm returning data from the adespresso api and it gives me a collection of arrays. I need all of the data to stay with their associated index but I'd like the "dates" array to go from oldest -> newest
(9) [Array(44), Array(44), Array(44), Array(44), Array(44), Array(44), Array(44), Array(44), Array(44)]
0: (44) [22, 45, 33, 14, 14, 16, 21, 8, 13, 38, 23, 8, 32, 34, 31, 33, 39, 40, 20, 27, 26, 25, 19, 18, 21, 16, 16, 17, 10, 22, 21, 12, 22, 24, 17, 31, 48, 3, 6, 30, 31, 25, 29, 30, __ob__: Observer]
1: (44) [18654, 45691, 40658, 36675, 15334, 14092, 16779, 7104, 29030, 36688, 28032, 9167, 36460, 29380, 68940, 41206, 39499, 41728, 18600, 46620, 40836, 18743, 14571, 20585, 37621, 20621, 25708, 24696, 23997, 23920, 23087, 16935, 23279, 24422, 22231, 26471, 22116, 1682, 5600, 26088, 23896, 20828, 21413, 21507, __ob__: Observer]
2: (44) [21411, 59945, 51623, 46025, 18660, 16244, 21814, 7803, 32546, 42462, 33126, 9455, 53619, 40792, 100527, 55077, 51717, 56272, 22392, 63162, 57521, 27301, 20097, 29707, 58701, 32894, 39353, 37776, 36172, 36599, 36749, 26163, 39397, 36404, 29434, 38367, 33085, 1770, 6235, 37685, 33798, 27707, 29400, 29520, __ob__: Observer]
3: (44) [83.4785714285719, 72.914130434783, 108.059111111111, 270.411, 33.075, 28.63, 31.029047619048, 23.1275, 83.3075, 20.015789473684, 34.146086956522, 28.81125, 145.239333333333, 110.290952380953, 145.977948717949, 145.92934065934, 147.91375, 114.64889180672301, 138.952666666667, 191.465214285714, 168.540357142857, 36.2192, 32.62, 38.801666666667, 46.124285714286, 56.34625, 56.340625, 53.214117647059, 89.842, 37.909090909091, 45.330476190476, 73.679166666667, 41.104545454545, 37.45375, 43.104705882353, 31.33935483871, 19.071875, 15.476666666667, 29.698333333333, 32.723666666667, 28.947419354839, 36.3032, 31.058620689655, 30.176666666667, __ob__: Observer]
4: (44) [515.26, 1713.21, 1770.25, 1451.85, 463.05, 458.08, 651.61, 185.02, 741.08, 760.6, 785.36, 230.49, 1680.29, 1188.43, 1691.55, 1793.4099999999999, 1709.2600000000002, 1721.1100000000001, 802.04, 1872.01, 1629.82, 905.48, 619.78, 698.43, 968.61, 901.54, 901.45, 904.64, 898.42, 834, 951.94, 884.15, 904.3, 898.89, 732.78, 971.52, 915.45, 46.43, 178.19, 981.71, 897.37, 907.58, 900.7, 905.3, __ob__: Observer]
5: (44) [4.6186522261657, 3.50743074533421, 1.9836424858165, 6.29536105513759, 0.91639871382637, 1.1881310022162, 1.4119372879802, 0.90990644623863, 1.6123935440389299, 0.94908388676935, 0.97204612690938, 1.2374405076679, 5.74154284998572, 4.6611200088598, 4.1220613934445, 4.2621435829858, 4.13869756589639, 3.7705192494076902, 4.50066253145964, 4.0812209831317, 3.17233907020251, 1.1354895425076, 1.3584117032393, 1.3532164136399, 1.3458033082912, 1.1005046513042, 1.3010443930577, 1.1091698432867, 0.95101183235652, 1.0437443646001, 1.1238401044926, 1.1160799602492, 1.0076909409346, 0.97516756400396, 1.0056397363593, 0.87835900643782, 1.1576243010428, 0.7909604519774, 1.2349639133921, 1.0614302772987, 0.88466773181845, 0.99613816003176, 0.92176870748299, 1.0670731707317, __ob__: Observer]
6: (44) [5.4843632704270995, 7.3536923588433005, 6.916814351145, 23.6046465439322, 2.7078947368421, 2.3734715025907, 2.1156168831169, 2.6059154929577, 7.0246969696969, 1.8873449131514, 2.4390062111801, 1.97, 7.333116586727701, 6.3772908599176, 5.3959220869002, 7.7942632650196, 7.7841515416576, 8.36957297856, 8.642372693657599, 8.561180971315, 9.217287722586601, 2.9209032258065, 2.2702564102564, 1.7373880597015, 1.2260886075949, 2.4904419889503, 1.76064453125, 2.1590453460621, 2.6116860465116, 2.1832460732984, 2.3049394673123, 3.0279109589041, 2.2778337531486, 2.5320845070423, 2.4756081081081, 2.8828486646884, 2.3902088772846, 3.3164285714286, 2.3141558441558, 2.454275, 3.001237458194, 3.2883333333333, 3.3236162361624, 2.8739682539683, __ob__: Observer]
7: (44) [247, 642, 512, 430, 171, 193, 308, 71, 237, 403, 322, 117, 609, 506, 1012, 614, 594, 554, 246, 578, 469, 310, 273, 402, 790, 362, 512, 419, 344, 382, 413, 292, 397, 355, 296, 337, 383, 14, 77, 400, 299, 276, 271, 315, __ob__: Observer]
8: (44) ["2017-09-13", "2017-09-20", "2017-09-27", "2017-10-04", "2017-08-09", "2017-08-16", "2017-08-23", "2017-08-30", "2018-05-23", "2018-05-30", "2018-06-06", "2018-06-13", "2017-10-11", "2017-10-18", "2017-10-25", "2017-11-01", "2017-11-08", "2017-11-15", "2017-11-22", "2017-11-29", "2017-12-06", "2017-12-13", "2017-12-20", "2017-12-27", "2018-01-03", "2018-01-10", "2018-01-17", "2018-01-24", "2018-01-31", "2018-02-07", "2018-02-14", "2018-02-21", "2018-02-28", "2018-03-07", "2018-03-14", "2018-03-21", "2018-03-28", "2018-04-04", "2018-04-11", "2018-04-18", "2018-04-25", "2018-05-02", "2018-05-09", "2018-05-16", __ob__: Observer]
0: "2017-09-13"1: "2017-09-20"2: "2017-09-27"3: "2017-10-04"4: "2017-08-09"5: "2017-08-16"6: "2017-08-23"7: "2017-08-30"8: "2018-05-23"9: "2018-05-30"10: "2018-06-06"11: "2018-06-13"12: "2017-10-11"13: "2017-10-18"14: "2017-10-25"15: "2017-11-01"16: "2017-11-08"17: "2017-11-15"18: "2017-11-22"19: "2017-11-29"20: "2017-12-06"21: "2017-12-13"22: "2017-12-20"23: "2017-12-27"24: "2018-01-03"25: "2018-01-10"26: "2018-01-17"27: "2018-01-24"28: "2018-01-31"29: "2018-02-07"30: "2018-02-14"31: "2018-02-21"32: "2018-02-28"33: "2018-03-07"34: "2018-03-14"35: "2018-03-21"36: "2018-03-28"37: "2018-04-04"38: "2018-04-11"39: "2018-04-18"40: "2018-04-25"41: "2018-05-02"42: "2018-05-09"43: "2018-05-16"length: 44__ob__: Observer {value: Array(44), dep: Dep, vmCount: 0}__proto__: Arraylength: 9__proto__: Array(0)
Is there a way to sort the dates but when sorting the date I'd have to sort the other arrays so their indexes matched.
I would prefer to do this on the backend in php but if not the array get's returned to the vue in javascript.
Here is a bit more code. So on multiple occasions I will get results back with the same dates, so I have to group them by dates. I do that via this:
$totals = array_map(null, $results, $reach, $impressions, $cpr, $spend, $ctr, $cpc, $clicks, $dates);
$filteredArrays = [];
foreach ($totals as $total){
if(isset($total[8])){
$used = false;
foreach($filteredArrays as $key => $filtered){
if($filtered[0][8] !== $total[8]) continue;
$used = true;
array_push($filteredArrays[$key], $total);
}
if(!$used) array_push($filteredArrays, [$total]);
}
}
return $filteredArrays;
Could I possibly do it there?
Upvotes: 2
Views: 85
Reputation: 28
PHP has a few neat functions that can help with this. First being array_multisort() which you can read about at http://php.net/manual/en/function.array-multisort.php
Using this function you can sort either multiple arrays at a time or, in your case, a multidimensional array.
Edit: This also works with associative arrays, if you assign one of the below arrays a key, and run the array_multisort() function the same way, it will still run as intended.
Ex:
$ar = array(
array(3, 5, 1, 2, 4),
array(1, 2, 3, 4, 5),
array(2, 4, 1, 5, 3),
array(0, 7, 8, 9, 6),
array("apples", "bananas", "organges", "pears", "pineapples")
);
array_multisort($ar[0], SORT_ASC, SORT_NUMERIC,
$ar[1],
$ar[2],
$ar[3],
$ar[4]);
echo "<br>";
var_dump($ar);
Which will sort all of the arrays by the first array ascending numerically and output as follows
array(5) {
[0]=> array(5) {
[0]=> int(1)
[1]=> int(2)
[2]=> int(3)
[3]=> int(4)
[4]=> int(5) }
[1]=> array(5) {
[0]=> int(3)
[1]=> int(4)
[2]=> int(1)
[3]=> int(5)
[4]=> int(2) }
[2]=> array(5) {
[0]=> int(1)
[1]=> int(5) [2]=> int(2)
[3]=> int(3)
[4]=> int(4) }
[3]=> array(5) {
[0]=> int(8)
[1]=> int(9)
[2]=> int(0)
[3]=> int(6)
[4]=> int(7) }
[4]=> array(5) {
[0]=> string(8) "organges"
[1]=> string(5) "pears"
[2]=> string(6) "apples"
[3]=> string(10) "pineapples"
[4]=> string(7) "bananas"
}
}
The problem you might face though is that the date is not an integer, and so you can't sort that easily. I'm not sure if it would work sorting it just as is, but like I said earlier, PHP has some neat functions for this kind of stuff. You can use the function strtotime() found at http://php.net/manual/en/function.strtotime.php This converts a date into the number of seconds since the unix epoch, January 1, 1970. Because we can't use this function inside of the multisort function, we'll have to make a new array and fill it with the unix time, and then append it to your array and sort with that.
$dateAsSec = array()
foreach($date as $timestamp) {
$dateAsSec[] = strtotime($timestamp);
}
$filteredArrays[] = $dateAsSec[]
$finalArray = array_multisort($filteredArrays[9], SORT_ASC, SORT_NUMERIC,
$filteredArrays[0],
$filteredArrays[1],
$filteredArrays[2],
$filteredArrays[3],
$filteredArrays[4],
$filteredArrays[5],
$filteredArrays[6],
$filteredArrays[7],
$filteredArrays[8]);
And now $finalArray should be equal to your sorted array. Also as a side note, I've found in my own projects in the past that php has tons of functions for dealing with time, so it's always a good idea to check those out when first before trying to analyze the time yourself.
Upvotes: 1