I am using PHP 7.3.5.
I have an array with emtpy spots, which I would like to fill up values from the previous spot:
For example:
0 => 'FriAug 1',
1 => '',
2 => '',
3 => '',
4 => '',
Should result in:
0 => '01.08.2019',
1 => '01.08.2019',
2 => '01.08.2019',
3 => '01.08.2019',
4 => '01.08.2019',
I tried the following:
<?php
$arr = array (
0 => 'FriAug 1',
1 => '',
2 => '',
3 => '',
4 => '',
5 => '',
6 => '',
7 => '',
8 => '',
9 => '',
10 => '',
11 => '',
12 => '',
13 => '',
14 => '',
15 => '',
16 => '',
17 => '',
18 => '',
19 => 'SatNov 2',
20 => 'SunNov 3',
21 => '',
22 => '',
23 => '',
24 => '',
25 => '',
26 => 'MonJan 4',
27 => '',
28 => '',
29 => '',
30 => '',
31 => '',
32 => '',
33 => '',
34 => '',
35 => '',
36 => '',
37 => '',
38 => '',
39 => '',
40 => '',
41 => '',
42 => '',
43 => '',
44 => '',
45 => 'TueDec 5',
46 => '',
47 => '',
48 => '',
49 => '',
50 => '',
51 => '',
52 => '',
53 => '',
54 => '',
55 => '',
56 => '',
57 => '',
58 => '',
59 => '',
60 => '',
61 => '',
62 => 'WedNov 6'
);
$resArr = array();
foreach ($arr as $key => $v) {
$prev = $key - 1;
if($arr[$key] === '') {
array_push($resArr, strtotime(preg_replace("^.{0,3}","", $arr[$prev])));
} else {
array_push($resArr, strtotime(preg_replace("^.{0,3}","", $arr[$key])));
}
}
print_r($resArr);
My final result should look like the following:
// Wanted Result:
0 => '01.08.2019',
1 => '01.08.2019',
2 => '01.08.2019',
3 => '01.08.2019',
4 => '01.08.2019',
5 => '01.08.2019',
6 => '01.08.2019',
7 => '01.08.2019',
8 => '01.08.2019',
9 => '01.08.2019',
10 => '01.08.2019',
11 => '01.08.2019',
12 => '01.08.2019',
13 => '01.08.2019',
14 => '01.08.2019',
15 => '01.08.2019',
16 => '01.08.2019',
17 => '01.08.2019',
18 => '01.08.2019',
19 => '02.11.2019',
20 => '03.11.2019',
21 => '03.11.2019',
22 => '03.11.2019',
23 => '03.11.2019',
24 => '03.11.2019',
25 => '03.11.2019',
26 => '04.11.2019',
27 => '04.11.2019',
28 => '04.11.2019',
29 => '04.11.2019',
30 => '04.11.2019',
31 => '04.11.2019',
32 => '04.11.2019',
33 => '04.11.2019',
34 => '04.11.2019',
35 => '04.11.2019',
36 => '04.11.2019',
37 => '04.11.2019',
38 => '04.11.2019',
39 => '04.11.2019',
40 => '04.11.2019',
41 => '04.11.2019',
42 => '04.11.2019',
43 => '04.11.2019',
44 => '04.11.2019',
45 => '05.12.2019',
46 => '05.12.2019',
47 => '05.12.2019',
48 => '05.12.2019',
49 => '05.12.2019',
50 => '05.12.2019',
51 => '05.12.2019',
52 => '05.12.2019',
53 => '05.12.2019',
54 => '05.12.2019',
55 => '05.12.2019',
56 => '05.12.2019',
57 => '05.12.2019',
58 => '05.12.2019',
59 => '05.12.2019',
60 => '05.12.2019',
61 => '05.12.2019',
62 => '06.11.2019'
);
However, I only get null/false values in my final output.
Any suggestions what I am doing wrong?
I appreciate your replies!
One of the problems is the use of the regex - your pattern isn't correct - but as it's just removing 3 chars of the front, it's easier to use substr() to remove them.
The second part is that you just look at the previous array position in the source array - which may also be blank. This version just stores the last value generated (in $prev) and keeps on using it when needed...
$resArr = array();
$prev = "";
foreach ($arr as $key => $v) {
if($arr[$key] === '') {
array_push($resArr, $prev);
} else {
$prev = date("d.m.Y",strtotime(substr($arr[$key], 3)));
array_push($resArr, $prev);
}
}
This could be simplified without losing too much clarity to...
$resArr = array();
$prev = "";
foreach ($arr as $key => $v) {
if($arr[$key] !== '') {
$prev = date("d.m.Y",strtotime(substr($arr[$key], 3)));
}
array_push($resArr, $prev);
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 months ago.
Improve this question
How to print option under selection_items
Array
(
[id] => 19
[is_core_field] => 0
[order_num] => 19
[name] => Accommodation Amenities
[slug] => accommodation_amenities
[description] =>
[type] => checkbox
[icon_image] =>
[is_required] => 0
[is_configuration_page] => 1
[is_search_configuration_page] => 1
[is_ordered] => 0
[is_hide_name] => 0
[for_admin_only] => 0
[on_exerpt_page] => 0
[on_listing_page] => 1
[on_search_form] => 1
[on_map] => 0
[advanced_search_form] => 0
[categories] => a:6:{i:0;s:4:"2801";i:1;s:4:"2874";i:2;s:4:"2852";i:3;s:4:"2849";i:4;s:4:"2850";i:5;s:4:"2851";}
[options] => a:4:{s:15:"selection_items";a:153:{i:5;s:23:"24-Hour Guest Reception";i:60;s:16:"24-Hour Security";i:138;s:18:"Accessible Parking";i:55;s:7:"Adapter";i:122;s:17:"Additional Toilet";i:30;s:16:"Air Conditioning";i:123;s:17:"Allergy-free room";i:142;s:11:"ATM On-Site";i:61;s:27:"Babysitting/child services ";i:46;s:7:"Balcony";i:48;s:8:"Bathrobe";i:45;s:7:"Bathtub";i:39;s:10:"Bar/Lounge";i:148;s:10:"Beachfront";i:84;s:9:"Body Soap";i:35;s:12:"Bridal Suite";i:13;s:17:"Business Services";i:25;s:18:"Cable/Satellite TV";i:27;s:4:"CCTV";i:56;s:20:"Cleaning Commodities";i:57;s:12:"Clothes Rack";i:14;s:23:"Complimentary Breakfast";i:6;s:24:"Complimentary Toiletries";i:32;s:9:"Concierge";i:116;s:11:"Conditioner";i:20;s:25:"Cribs & Cots for Children";i:22;s:19:"Curated Experiences";i:21;s:13:"Custom Offers";i:78;s:23:"Designated Smoking Area";i:132;s:8:"Detached";i:90;s:11:"Dining Area";i:118;s:23:"Drying Rack for Clothes";i:64;s:15:"Electric Kettle";i:11;s:20:"Electronics Chargers";i:36;s:15:"Elevator Access";i:114;s:35:"Entire Unit located on Ground Floor";i:17;s:25:"Exercise Facilities (Gym)";i:40;s:23:"Executive Lounge Access";i:96;s:26:"Express Check in/Check Out";i:92;s:12:"Family Rooms";i:49;s:3:"Fan";i:113;s:14:"Feather Pillow";i:44;s:17:"Fire Extinguisher";i:155;s:13:"First Aid Kit";i:8;s:17:"Flexible Checkout";i:152;s:44:"Food can be Delivered to Guest Accommodation";i:26;s:19:"Free Early Check-in";i:3;s:12:"Free Parking";i:2;s:9:"Free WIFI";i:77;s:6:"Fruits";i:93;s:6:"Garden";i:130;s:11:"Garden View";i:71;s:9:"Gift shop";i:115;s:5:"Grill";i:83;s:9:"Hairdryer";i:131;s:38:"Hand sanitizer in guest accommodation ";i:124;s:26:"Hardwood or Parquet Floors";i:7;s:17:"Healthy Breakfast";i:149;s:9:"Hot Water";i:157;s:7:"Heating";i:24;s:12:"Housekeeping";i:58;s:21:"Hypoallergenic pillow";i:103;s:23:"Increased Accessibility";i:125;s:20:"Inner Courtyard View";i:62;s:32:"Interconnected room(s) available";i:137;s:22:"Iron and Ironing Board";i:99;s:15:"Ironing Service";i:38;s:15:"Key Card Access";i:41;s:10:"Kids Meals";i:128;s:19:"Kid-friendly Buffet";i:156;s:28:"Kid activities/ Kid Friendly";i:33;s:11:"Kitchenette";i:94;s:13:"Landmark View";i:150;s:11:"Laptop Safe";i:15;s:16:"Laundry Services";i:67;s:6:"Linens";i:146;s:7:"Lockers";i:76;s:26:"Meeting/Banquet facilities";i:29;s:9:"Microwave";i:63;s:13:"Mountain View";i:89;s:12:"Mosquito Net";i:18;s:9:"Newspaper";i:54;s:15:"No Pets Allowed";i:120;s:22:"No Single-Use Plastics";i:53;s:10:"No Smoking";i:109;s:18:"Non-Feather Pillow";i:119;s:27:"Non-Smoking Rooms Available";i:28;s:18:"On-site Restaurant";i:70;s:17:"Outdoor furniture";i:98;s:19:"Outdoor Dining Area";i:121;s:4:"Oven";i:153;s:17:"Outdoor Fireplace";i:95;s:14:"Packed Lunches";i:80;s:11:"Pants Press";i:23;s:18:"Pet-friendly Rooms";i:105;s:11:"Picnic Area";i:85;s:25:"Physical Distancing Rules";i:133;s:29:"Private Apartment in Building";i:134;s:16:"Private Bathroom";i:136;s:14:"Private Beach ";i:91;s:26:"Private Check-In/Check-Out";i:72;s:16:"Private Entrance";i:140;s:17:"Quiet Street View";i:144;s:10:"Restaurant";i:10;s:12:"Refrigerator";i:129;s:10:"River View";i:145;s:7:"Rooftop";i:4;s:12:"Room Service";i:65;s:4:"Safe";i:34;s:18:"Safety Deposit Box";i:147;s:5:"Sauna";i:73;s:8:"Sea view";i:74;s:12:"Seating Area";i:100;s:15:"Secured Parking";i:112;s:13:"Semi-detached";i:101;s:15:"Shared Bathroom";i:102;s:21:"Shared Lounge/TV Area";i:66;s:7:"Shampoo";i:106;s:9:"Shoeshine";i:68;s:6:"Shower";i:69;s:10:"Shower cap";i:31;s:15:"Shuttle Service";i:37;s:12:"Smoke Alarms";i:81;s:9:"Snack Bar";i:88;s:19:"Socket near the bed";i:50;s:4:"Sofa";i:16;s:24:"Spa & Wellness Amenities";i:59;s:18:"Special Diet Menus";i:154;s:66:"Staff follow all safety protocols as directed by local authorities";i:19;s:18:"Storage Available ";i:111;s:8:"Stovetop";i:97;s:14:"Street Parking";i:9;s:13:"Swimming Pool";i:86;s:16:"Tea/coffee maker";i:43;s:9:"Telephone";i:51;s:13:"Terrace/Patio";i:107;s:17:"Tile/Marble Floor";i:108;s:7:"Toaster";i:126;s:12:"Toilet Paper";i:139;s:10:"Toiletries";i:42;s:6:"Towels";i:143;s:2:"TV";i:75;s:10:"Trash Cans";i:117;s:33:"Upper Floors accessible by Stairs";i:104;s:13:"Valet Parking";i:151;s:19:"VIP Room Facilities";i:47;s:14:"Walk-in Shower";i:141;s:15:"Washing Machine";i:52;s:15:"Wardrobe/Closet";i:79;s:16:"Wedding services";i:82;s:6:"Window";i:135;s:12:"Wine Glasses";i:1;s:18:"Wheel Chair Access";}s:11:"icon_images";a:153:{i:5;s:0:"";i:60;s:0:"";i:138;s:0:"";i:55;s:0:"";i:122;s:0:"";i:30;s:0:"";i:123;s:0:"";i:142;s:0:"";i:61;s:0:"";i:46;s:0:"";i:48;s:0:"";i:45;s:0:"";i:39;s:0:"";i:148;s:0:"";i:84;s:0:"";i:35;s:0:"";i:13;s:0:"";i:25;s:0:"";i:27;s:0:"";i:56;s:0:"";i:57;s:0:"";i:14;s:0:"";i:6;s:0:"";i:32;s:0:"";i:116;s:0:"";i:20;s:0:"";i:22;s:0:"";i:21;s:0:"";i:78;s:0:"";i:132;s:0:"";i:90;s:0:"";i:118;s:0:"";i:64;s:0:"";i:11;s:0:"";i:36;s:0:"";i:114;s:0:"";i:17;s:0:"";i:40;s:0:"";i:96;s:0:"";i:92;s:0:"";i:49;s:0:"";i:113;s:0:"";i:44;s:0:"";i:155;s:0:"";i:8;s:0:"";i:152;s:0:"";i:26;s:0:"";i:3;s:0:"";i:2;s:0:"";i:77;s:0:"";i:93;s:0:"";i:130;s:0:"";i:71;s:0:"";i:115;s:0:"";i:83;s:0:"";i:131;s:0:"";i:124;s:0:"";i:7;s:0:"";i:149;s:0:"";i:157;s:0:"";i:24;s:0:"";i:58;s:0:"";i:103;s:0:"";i:125;s:0:"";i:62;s:0:"";i:137;s:0:"";i:99;s:0:"";i:38;s:0:"";i:41;s:0:"";i:128;s:0:"";i:156;s:0:"";i:33;s:0:"";i:94;s:0:"";i:150;s:0:"";i:15;s:0:"";i:67;s:0:"";i:146;s:0:"";i:76;s:0:"";i:29;s:0:"";i:63;s:0:"";i:89;s:0:"";i:18;s:0:"";i:54;s:0:"";i:120;s:0:"";i:53;s:0:"";i:109;s:0:"";i:119;s:0:"";i:28;s:0:"";i:70;s:0:"";i:98;s:0:"";i:121;s:0:"";i:153;s:0:"";i:95;s:0:"";i:80;s:0:"";i:23;s:0:"";i:105;s:0:"";i:85;s:0:"";i:133;s:0:"";i:134;s:0:"";i:136;s:0:"";i:91;s:0:"";i:72;s:0:"";i:140;s:0:"";i:144;s:0:"";i:10;s:0:"";i:129;s:0:"";i:145;s:0:"";i:4;s:0:"";i:65;s:0:"";i:34;s:0:"";i:147;s:0:"";i:73;s:0:"";i:74;s:0:"";i:100;s:0:"";i:112;s:0:"";i:101;s:0:"";i:102;s:0:"";i:66;s:0:"";i:106;s:0:"";i:68;s:0:"";i:69;s:0:"";i:31;s:0:"";i:37;s:0:"";i:81;s:0:"";i:88;s:0:"";i:50;s:0:"";i:16;s:0:"";i:59;s:0:"";i:154;s:0:"";i:19;s:0:"";i:111;s:0:"";i:97;s:0:"";i:9;s:0:"";i:86;s:0:"";i:43;s:0:"";i:51;s:0:"";i:107;s:0:"";i:108;s:0:"";i:126;s:0:"";i:139;s:0:"";i:42;s:0:"";i:143;s:0:"";i:75;s:0:"";i:117;s:0:"";i:104;s:0:"";i:151;s:0:"";i:47;s:0:"";i:141;s:0:"";i:52;s:0:"";i:79;s:0:"";i:82;s:0:"";i:135;s:0:"";i:1;s:0:"";}s:17:"how_display_items";s:7:"checked";s:14:"columns_number";s:1:"3";}
[search_options] =>
[group_id] => 0
)
How can i get the result from options =>"selection_items". Please guide me, i am using wordpress. thanks in advance
WordPress serializes arrays and objexts when stored in the database.
You need to unserialize the value of options. For example:
unserialize($array['options']);
WordPress also includes a maybe_unserialize function that does the same job, but it includes a condition to only unserialize data that has been serialized.
maybe_unserialize($array['options']);
Once you've unserialized the options it will return the following array:
array (
'selection_items' =>
array (
5 => '24-Hour Guest Reception',
60 => '24-Hour Security',
138 => 'Accessible Parking',
55 => 'Adapter',
122 => 'Additional Toilet',
30 => 'Air Conditioning',
123 => 'Allergy-free room',
142 => 'ATM On-Site',
61 => 'Babysitting/child services ',
46 => 'Balcony',
48 => 'Bathrobe',
45 => 'Bathtub',
39 => 'Bar/Lounge',
148 => 'Beachfront',
84 => 'Body Soap',
35 => 'Bridal Suite',
13 => 'Business Services',
25 => 'Cable/Satellite TV',
27 => 'CCTV',
56 => 'Cleaning Commodities',
57 => 'Clothes Rack',
14 => 'Complimentary Breakfast',
6 => 'Complimentary Toiletries',
32 => 'Concierge',
116 => 'Conditioner',
20 => 'Cribs & Cots for Children',
22 => 'Curated Experiences',
21 => 'Custom Offers',
78 => 'Designated Smoking Area',
132 => 'Detached',
90 => 'Dining Area',
118 => 'Drying Rack for Clothes',
64 => 'Electric Kettle',
11 => 'Electronics Chargers',
36 => 'Elevator Access',
114 => 'Entire Unit located on Ground Floor',
17 => 'Exercise Facilities (Gym)',
40 => 'Executive Lounge Access',
96 => 'Express Check in/Check Out',
92 => 'Family Rooms',
49 => 'Fan',
113 => 'Feather Pillow',
44 => 'Fire Extinguisher',
155 => 'First Aid Kit',
8 => 'Flexible Checkout',
152 => 'Food can be Delivered to Guest Accommodation',
26 => 'Free Early Check-in',
3 => 'Free Parking',
2 => 'Free WIFI',
77 => 'Fruits',
93 => 'Garden',
130 => 'Garden View',
71 => 'Gift shop',
115 => 'Grill',
83 => 'Hairdryer',
131 => 'Hand sanitizer in guest accommodation ',
124 => 'Hardwood or Parquet Floors',
7 => 'Healthy Breakfast',
149 => 'Hot Water',
157 => 'Heating',
24 => 'Housekeeping',
58 => 'Hypoallergenic pillow',
103 => 'Increased Accessibility',
125 => 'Inner Courtyard View',
62 => 'Interconnected room(s) available',
137 => 'Iron and Ironing Board',
99 => 'Ironing Service',
38 => 'Key Card Access',
41 => 'Kids Meals',
128 => 'Kid-friendly Buffet',
156 => 'Kid activities/ Kid Friendly',
33 => 'Kitchenette',
94 => 'Landmark View',
150 => 'Laptop Safe',
15 => 'Laundry Services',
67 => 'Linens',
146 => 'Lockers',
76 => 'Meeting/Banquet facilities',
29 => 'Microwave',
63 => 'Mountain View',
89 => 'Mosquito Net',
18 => 'Newspaper',
54 => 'No Pets Allowed',
120 => 'No Single-Use Plastics',
53 => 'No Smoking',
109 => 'Non-Feather Pillow',
119 => 'Non-Smoking Rooms Available',
28 => 'On-site Restaurant',
70 => 'Outdoor furniture',
98 => 'Outdoor Dining Area',
121 => 'Oven',
153 => 'Outdoor Fireplace',
95 => 'Packed Lunches',
80 => 'Pants Press',
23 => 'Pet-friendly Rooms',
105 => 'Picnic Area',
85 => 'Physical Distancing Rules',
133 => 'Private Apartment in Building',
134 => 'Private Bathroom',
136 => 'Private Beach ',
91 => 'Private Check-In/Check-Out',
72 => 'Private Entrance',
140 => 'Quiet Street View',
144 => 'Restaurant',
10 => 'Refrigerator',
129 => 'River View',
145 => 'Rooftop',
4 => 'Room Service',
65 => 'Safe',
34 => 'Safety Deposit Box',
147 => 'Sauna',
73 => 'Sea view',
74 => 'Seating Area',
100 => 'Secured Parking',
112 => 'Semi-detached',
101 => 'Shared Bathroom',
102 => 'Shared Lounge/TV Area',
66 => 'Shampoo',
106 => 'Shoeshine',
68 => 'Shower',
69 => 'Shower cap',
31 => 'Shuttle Service',
37 => 'Smoke Alarms',
81 => 'Snack Bar',
88 => 'Socket near the bed',
50 => 'Sofa',
16 => 'Spa & Wellness Amenities',
59 => 'Special Diet Menus',
154 => 'Staff follow all safety protocols as directed by local authorities',
19 => 'Storage Available ',
111 => 'Stovetop',
97 => 'Street Parking',
9 => 'Swimming Pool',
86 => 'Tea/coffee maker',
43 => 'Telephone',
51 => 'Terrace/Patio',
107 => 'Tile/Marble Floor',
108 => 'Toaster',
126 => 'Toilet Paper',
139 => 'Toiletries',
42 => 'Towels',
143 => 'TV',
75 => 'Trash Cans',
117 => 'Upper Floors accessible by Stairs',
104 => 'Valet Parking',
151 => 'VIP Room Facilities',
47 => 'Walk-in Shower',
141 => 'Washing Machine',
52 => 'Wardrobe/Closet',
79 => 'Wedding services',
82 => 'Window',
135 => 'Wine Glasses',
1 => 'Wheel Chair Access',
),
'icon_images' =>
array (
5 => '',
60 => '',
138 => '',
55 => '',
122 => '',
30 => '',
123 => '',
142 => '',
61 => '',
46 => '',
48 => '',
45 => '',
39 => '',
148 => '',
84 => '',
35 => '',
13 => '',
25 => '',
27 => '',
56 => '',
57 => '',
14 => '',
6 => '',
32 => '',
116 => '',
20 => '',
22 => '',
21 => '',
78 => '',
132 => '',
90 => '',
118 => '',
64 => '',
11 => '',
36 => '',
114 => '',
17 => '',
40 => '',
96 => '',
92 => '',
49 => '',
113 => '',
44 => '',
155 => '',
8 => '',
152 => '',
26 => '',
3 => '',
2 => '',
77 => '',
93 => '',
130 => '',
71 => '',
115 => '',
83 => '',
131 => '',
124 => '',
7 => '',
149 => '',
157 => '',
24 => '',
58 => '',
103 => '',
125 => '',
62 => '',
137 => '',
99 => '',
38 => '',
41 => '',
128 => '',
156 => '',
33 => '',
94 => '',
150 => '',
15 => '',
67 => '',
146 => '',
76 => '',
29 => '',
63 => '',
89 => '',
18 => '',
54 => '',
120 => '',
53 => '',
109 => '',
119 => '',
28 => '',
70 => '',
98 => '',
121 => '',
153 => '',
95 => '',
80 => '',
23 => '',
105 => '',
85 => '',
133 => '',
134 => '',
136 => '',
91 => '',
72 => '',
140 => '',
144 => '',
10 => '',
129 => '',
145 => '',
4 => '',
65 => '',
34 => '',
147 => '',
73 => '',
74 => '',
100 => '',
112 => '',
101 => '',
102 => '',
66 => '',
106 => '',
68 => '',
69 => '',
31 => '',
37 => '',
81 => '',
88 => '',
50 => '',
16 => '',
59 => '',
154 => '',
19 => '',
111 => '',
97 => '',
9 => '',
86 => '',
43 => '',
51 => '',
107 => '',
108 => '',
126 => '',
139 => '',
42 => '',
143 => '',
75 => '',
117 => '',
104 => '',
151 => '',
47 => '',
141 => '',
52 => '',
79 => '',
82 => '',
135 => '',
1 => '',
),
'how_display_items' => 'checked',
'columns_number' => '3',
)
I'm trying to count integer value from JSON: https://pomber.github.io/covid19/timeseries.json and got what I expected.
{
"date": [
"22 Jan",
"23 Jan",
"24 Jan",
"25 Jan",
....
],
"total_confirmed": [
555,
653,
941,
1434,
....
],
"total_deaths": [
17,
18,
26,
42,
....
],
"total_recovered": [
28,
30,
36,
39,
....
],
"max_value_of_total_confirmed": 272166
}
Here's my controller:
$client = new Client();$request = $client->get('https://pomber.github.io/covid19/timeseries.json');
$response = $request->getBody()->getContents();
$posts_dates = json_decode($response, true);
$confirmed_array = array();
$deaths_array = array();
$recovered_array = array();
if ( ! empty( $posts_dates ) ) {
foreach ( $posts_dates as $country => $data ) {
foreach ( $data as $dataKey => $dateData ) {
$date = new \DateTime( $dateData['date'] );
$day = $date->format( 'd M' );
if ( !isset($confirmed_array[$day]) ) {
$confirmed_array[$day] = 0;
}
$confirmed_array[$day] += $dateData['confirmed'];
}
foreach ( $data as $dataKey => $dateData ) {
$date = new \DateTime( $dateData['date'] );
$day = $date->format( 'd M' );
if ( !isset($deaths_array[$day]) ) {
$deaths_array[$day] = 0;
}
$deaths_array[$day] += $dateData['deaths'];
}
foreach ( $data as $dataKey => $dateData ) {
$date = new \DateTime( $dateData['date'] );
$day = $date->format( 'd M' );
if ( !isset($recovered_array[$day]) ) {
$recovered_array[$day] = 0;
}
$recovered_array[$day] += $dateData['recovered'];
}
}
}
$output = [
"date" => array_keys($confirmed_array),
"total_confirmed" => array_values($confirmed_array),
"total_deaths" => array_values($deaths_array),
"total_recovered" => array_values($recovered_array),
"max_value_of_total_confirmed" => max($confirmed_array)
];
return $output;
What I'm trying to get is grouping those data not by date, but by week of the month (W3 Jan, W4 Jan, W1 Feb, W2 Feb, etc). Any help would be appreciated :)
Thank you.
I would recommend using collection. You can easily manage using the methods provided by Laravel.
Group By Day
$client = new Client();
$request = $client->get('https://pomber.github.io/covid19/timeseries.json');
$response = $request->getBody()->getContents();
$posts_dates = json_decode($response, true);
/****** Here we go ******/
$posts_dates = collect($posts_dates)
->flatten(1)
->groupBy('date')
->map(function ($item, $key) {
$item = collect($item);
$date = \Carbon\Carbon::parse($key);
$key = 'W' . $date->weekOfMonth . $date->format(' M');
return [
'date' => $key,
'total_confirmed' => $item->sum('confirmed'),
'total_deaths' => $item->sum('deaths'),
'total_recovered' => $item->sum('recovered'),
];
})
->groupBy(function ($item, $key) {
return collect($item)->keys()->all();
})
->map(function ($item, $key) {
return $item->map(function ($item) use ($key) {
return $item[$key];
});
});
$posts_dates->put('max_value_of_total_confirmed', $posts_dates['total_confirmed']->max());
dd($posts_dates->toArray());
Result
array:5 [
"date" => array:59 [
0 => "W4 Jan"
1 => "W4 Jan"
2 => "W4 Jan"
3 => "W4 Jan"
4 => "W4 Jan"
5 => "W4 Jan"
6 => "W4 Jan"
7 => "W5 Jan"
8 => "W5 Jan"
9 => "W5 Jan"
10 => "W1 Feb"
11 => "W1 Feb"
12 => "W1 Feb"
13 => "W1 Feb"
14 => "W1 Feb"
15 => "W1 Feb"
16 => "W1 Feb"
17 => "W2 Feb"
18 => "W2 Feb"
19 => "W2 Feb"
20 => "W2 Feb"
21 => "W2 Feb"
22 => "W2 Feb"
23 => "W2 Feb"
24 => "W3 Feb"
25 => "W3 Feb"
26 => "W3 Feb"
27 => "W3 Feb"
28 => "W3 Feb"
29 => "W3 Feb"
30 => "W3 Feb"
31 => "W4 Feb"
32 => "W4 Feb"
33 => "W4 Feb"
34 => "W4 Feb"
35 => "W4 Feb"
36 => "W4 Feb"
37 => "W4 Feb"
38 => "W5 Feb"
39 => "W1 Mar"
40 => "W1 Mar"
41 => "W1 Mar"
42 => "W1 Mar"
43 => "W1 Mar"
44 => "W1 Mar"
45 => "W1 Mar"
46 => "W2 Mar"
47 => "W2 Mar"
48 => "W2 Mar"
49 => "W2 Mar"
50 => "W2 Mar"
51 => "W2 Mar"
52 => "W2 Mar"
53 => "W3 Mar"
54 => "W3 Mar"
55 => "W3 Mar"
56 => "W3 Mar"
57 => "W3 Mar"
58 => "W3 Mar"
]
"total_confirmed" => array:59 [
0 => 555
1 => 653
2 => 941
3 => 1434
4 => 2118
5 => 2927
6 => 5578
7 => 6166
8 => 8234
9 => 9927
10 => 12038
11 => 16787
12 => 19881
13 => 23892
14 => 27635
15 => 30817
16 => 34391
17 => 37120
18 => 40150
19 => 42762
20 => 44802
21 => 45221
22 => 60368
23 => 66885
24 => 69030
25 => 71224
26 => 73258
27 => 75136
28 => 75639
29 => 76197
30 => 76823
31 => 78579
32 => 78965
33 => 79568
34 => 80413
35 => 81395
36 => 82754
37 => 84120
38 => 86011
39 => 88369
40 => 90306
41 => 92840
42 => 95120
43 => 97882
44 => 101784
45 => 105821
46 => 109795
47 => 113561
48 => 118592
49 => 125865
50 => 128343
51 => 145193
52 => 156094
53 => 167446
54 => 181527
55 => 197142
56 => 214910
57 => 242708
58 => 272166
]
"total_deaths" => array:59 [
0 => 17
1 => 18
2 => 26
3 => 42
4 => 56
5 => 82
6 => 131
7 => 133
8 => 171
9 => 213
10 => 259
11 => 362
12 => 426
13 => 492
14 => 564
15 => 634
16 => 719
17 => 806
18 => 906
19 => 1013
20 => 1113
21 => 1118
22 => 1371
23 => 1523
24 => 1666
25 => 1770
26 => 1868
27 => 2007
28 => 2122
29 => 2247
30 => 2251
31 => 2458
32 => 2469
33 => 2629
34 => 2708
35 => 2770
36 => 2814
37 => 2872
38 => 2941
39 => 2996
40 => 3085
41 => 3160
42 => 3254
43 => 3348
44 => 3460
45 => 3558
46 => 3802
47 => 3988
48 => 4262
49 => 4615
50 => 4720
51 => 5404
52 => 5819
53 => 6440
54 => 7126
55 => 7905
56 => 8733
57 => 9867
58 => 11299
]
"total_recovered" => array:59 [
0 => 28
1 => 30
2 => 36
3 => 39
4 => 52
5 => 61
6 => 107
7 => 126
8 => 143
9 => 222
10 => 284
11 => 472
12 => 623
13 => 852
14 => 1124
15 => 1487
16 => 2011
17 => 2616
18 => 3244
19 => 3946
20 => 4683
21 => 5150
22 => 6295
23 => 8058
24 => 9395
25 => 10865
26 => 12583
27 => 14352
28 => 16121
29 => 18177
30 => 18890
31 => 22886
32 => 23394
33 => 25227
34 => 27905
35 => 30384
36 => 33277
37 => 36711
38 => 39782
39 => 42716
40 => 45602
41 => 48228
42 => 51170
43 => 53796
44 => 55865
45 => 58358
46 => 60694
47 => 62494
48 => 64404
49 => 67003
50 => 68324
51 => 70251
52 => 72624
53 => 76034
54 => 78088
55 => 80840
56 => 83207
57 => 84854
58 => 87256
]
"max_value_of_total_confirmed" => 272166
]
Group By Week
$client = new Client();
$request = $client->get('https://pomber.github.io/covid19/timeseries.json');
$response = $request->getBody()->getContents();
$posts_dates = json_decode($response, true);
/****** Here we go ******/
$posts_dates = collect($posts_dates)
->flatten(1)
->map(function ($item) {
$date = \Carbon\Carbon::parse($item['date']);
$item['week'] = 'W' . $date->weekOfMonth . $date->format(' M');
return $item;
})
->groupBy('week')
->map(function ($item, $key) {
$item = collect($item);
return [
'date' => $key,
'total_confirmed' => $item->sum('confirmed'),
'total_deaths' => $item->sum('deaths'),
'total_recovered' => $item->sum('recovered'),
];
})
->groupBy(function ($item, $key) {
return collect($item)->keys()->all();
})
->map(function ($item, $key) {
return $item->map(function ($item) use ($key) {
return $item[$key];
});
});
$posts_dates->put('max_value_of_total_confirmed', $posts_dates['total_confirmed']->max());
dd($posts_dates->toArray());
Result
array:5 [
"date" => array:10 [
0 => "W4 Jan"
1 => "W5 Jan"
2 => "W1 Feb"
3 => "W2 Feb"
4 => "W3 Feb"
5 => "W4 Feb"
6 => "W5 Feb"
7 => "W1 Mar"
8 => "W2 Mar"
9 => "W3 Mar"
]
"total_confirmed" => array:10 [
0 => 14206
1 => 24327
2 => 165441
3 => 337308
4 => 517307
5 => 565794
6 => 86011
7 => 672122
8 => 897443
9 => 1275899
]
"total_deaths" => array:10 [
0 => 372
1 => 517
2 => 3456
3 => 7850
4 => 13931
5 => 18720
6 => 2941
7 => 22861
8 => 32610
9 => 51370
]
"total_recovered" => array:10 [
0 => 353
1 => 491
2 => 6853
3 => 33992
4 => 100383
5 => 199784
6 => 39782
7 => 355735
8 => 465794
9 => 490279
]
"max_value_of_total_confirmed" => 1275899
]
Below is what I am trying to accomplish
Array
(
[0.94] => 35
[0.93] => 36
[0.92] => 37
[0.91] => 38
.
.
.
[0.50] => 79
)
instead of building
$arrayName1 = array('0.94' => '35',
'0.93' => '36',
'0.92' => '37',
'0.91' => '38',
'0.50' => '79'
);
I'd like to know if there's a way to add loop to it to simplify process. Anyone?
With range() and array_combine() functions:
$result = array_combine(range(0.94, 0.5, -0.01), range(35, 79));
The $result contains:
Array
(
[0.94] => 35
[0.93] => 36
[0.92] => 37
[0.91] => 38
[0.9] => 39
[0.89] => 40
[0.88] => 41
[0.87] => 42
[0.86] => 43
[0.85] => 44
[0.84] => 45
[0.83] => 46
[0.82] => 47
[0.81] => 48
[0.8] => 49
[0.79] => 50
[0.78] => 51
[0.77] => 52
[0.76] => 53
[0.75] => 54
[0.74] => 55
[0.73] => 56
[0.72] => 57
[0.71] => 58
[0.7] => 59
[0.69] => 60
[0.68] => 61
[0.67] => 62
[0.66] => 63
[0.65] => 64
[0.64] => 65
[0.63] => 66
[0.62] => 67
[0.61] => 68
[0.6] => 69
[0.59] => 70
[0.58] => 71
[0.57] => 72
[0.56] => 73
[0.55] => 74
[0.54] => 75
[0.53] => 76
[0.52] => 77
[0.51] => 78
[0.5] => 79
)
You could simply loop.
$data = array();
$i = 0.94;
$j = 35;
while($i > 0.50){
$data[(string)$i] = $j;
$i -= 0.01;
$j++;
}
var_dump($data);
I experience a weird issue with for loop I mean I have associative array with makes:
$makes = array(0 => '9FF',
1 => 'AC',
2 => 'AMG',
3 => 'Alfa Romeo',
4 => 'Alpina',
5 => 'Ariel',
6 => 'Ascari',
7 => 'Aston Martin',
8 => 'Audi',
9 => 'B Engineering',
10 => 'BAC',
11 => 'BMW',
12 => 'Beck',
13 => 'Bentley',
14 => 'Bertone',
15 => 'Bizzarrini',
16 => 'Brabus',
17 => 'Bristol',
18 => 'Brooke',
19 => 'Bugatti',
20 => 'Buick',
21 => 'Cadillac',
22 => 'Callaway',
23 => 'Caparo',
24 => 'Caterham',
25 => 'Chevrolet',
26 => 'Chrysler',
27 => 'Citroen',
28 => 'Cizeta Moroder',
29 => 'Connaught',
30 => 'Covini',
31 => 'Dauer',
32 => 'David Brown',
33 => 'Dax',
34 => 'De Lorean',
35 => 'De Tomaso',
36 => 'Dodge',
37 => 'Dome',
38 => 'ES Motorsports',
39 => 'Elfin',
40 => 'Ferrari',
41 => 'Fiat',
42 => 'Fioravanti',
43 => 'Fisker',
44 => 'Ford',
45 => 'GM',
46 => 'GTA',
47 => 'Gemballa',
48 => 'Ghia',
49 => 'Ginetta',
50 => 'Giugiaro',
51 => 'Grinnall',
52 => 'Gumpert',
53 => 'Hennessey',
54 => 'Heuliez',
55 => 'Honda',
56 => 'Invicta',
57 => 'Isdera',
58 => 'Iso',
59 => 'Ital Design',
60 => 'Jaguar',
61 => 'Jeep',
62 => 'Jensen',
63 => 'Joss',
64 => 'KTM',
65 => 'Keio University',
66 => 'Koenig',
67 => 'Koenigsegg',
68 => 'Lamborghini',
69 => 'Lancia',
70 => 'Land Rover',
71 => 'LeBlanc',
72 => 'Leading Edge',
73 => 'Lexus',
74 => 'Light Car Company',
75 => 'Lingenfelter',
76 => 'Lister',
77 => 'Lotec',
78 => 'Lotus',
79 => 'MB Roadcars',
80 => 'MG',
81 => 'Marcos',
82 => 'Maserati',
83 => 'Maybach',
84 => 'Mazda',
85 => 'McLaren',
86 => 'Melling',
87 => 'Mercedes',
88 => 'Mitsubishi',
89 => 'Monteverdi',
90 => 'Morgan',
91 => 'Mosler',
92 => 'Nissan',
93 => 'Noble',
94 => 'Oldsmobile',
95 => 'Pagani',
96 => 'Palmer',
97 => 'Panoz',
98 => 'Panther',
99 => 'Peugeot',
100 => 'Pininfarina',
101 => 'Plymouth',
102 => 'Pontiac',
103 => 'Porsche',
104 => 'Prodrive',
105 => 'Radical',
106 => 'Renault',
107 => 'Rimac',
108 => 'Rolls Royce',
109 => 'Roush',
110 => 'Ruf',
111 => 'SCG',
112 => 'SSC',
113 => 'Saab',
114 => 'Saleen',
115 => 'Sbarro',
116 => 'Schuppan',
117 => 'Sin',
118 => 'Spectre',
119 => 'Spyker',
120 => 'Stealth',
121 => 'Strathcarron',
122 => 'Studiotorino',
123 => 'Subaru',
124 => 'Superformance',
125 => 'TVR',
126 => 'Techart',
127 => 'Tesla',
128 => 'Tiger',
129 => 'Toroidion',
130 => 'Toyota',
131 => 'Tramontana',
132 => 'Trident',
133 => 'UVA',
134 => 'Ultima',
135 => 'Vauxhall',
136 => 'Vector',
137 => 'Vemac',
138 => 'Venturi',
139 => 'Veritas',
140 => 'Volkswagen',
141 => 'Volvo',
142 => 'Westfield',
143 => 'Wiesmann',
144 => 'Wolfrace',
145 => 'Yamaha',
146 => 'Zagato',
147 => 'Zenvo');
and some other code:
$pics = '';
$logos = "http://www.supercarworld.com/images/logos/";
$gif = ".gif";
for ($x = 0; $x < count($makes); $x++) {
$pics = array($logos.$makes[$x].$gif);
print_r($pics[$x]);
$car = new Car();
$car->setName('http://www.supercarworld.com/images/fullpics/595.jpg');
// em entity manager
$em = $this->getDoctrine()->getManager();
$em->persist($car);
$em->flush();
$car = $this->getDoctrine()
->getRepository('GrupaProjektBundle:Car')
->findOneBy(array('id' => $car->getId(), 'name' => $car->getName()));
return $this->render('GrupaProjektBundle:Sc:supercars.html.twig', array('car' => $car,
'pics' => $pics[$x],
));
}
and when I want to get [$x] the behaviour is weird it displays only zero element i.e 9FF but when I get [0] or [1] from $makes it works
Why?
I want to make it to iterate am I missing some incrementation?
$pics is not an array, or the way you are assigning it, it is an array with exactly one element & will get overwritten in each iteration of your for loop.
Did you mean to do that, or did you want
$pics = array();
Then in your for loop:
$pics[$x] = $logos.$makes[$x].$gif;
I'm guessing here as it's not immediately clear what you're trying to achieve. But I can certainly say you're overwriting $pics on each iteration of your for loop.
i have a mp3 class to read mp3s for my site. (i cannot install the module since its a shared hosting). i upload the mp3 and then the system read it using my class and insert into my mysql the file name and location, and the basic tag (artist, song name, album). the mysql insert is ok but i have problem with the strings i am inserting.
here's my code: http://pastebin.com/fXsm0c3T
<?php
class Id3 {
private $tags = array(
'TALB' => 'album', 'TCON' => 'genre', 'TENC' => 'encoder',
'TIT2' => 'title', 'TPE1' => 'artist', 'TPE2' => 'ensemble', 'TYER' => 'year', 'TCOM' => 'composer',
'TCOP' => 'copyright', 'TRCK' => 'track', 'WXXX' => 'url',
'COMM' => 'comment'
);
private $genre = array(
0 => 'Blues', 1 => 'Classic Rock', 2 => 'Country', 3 => 'Dance', 4 => 'Disco', 5 => 'Funk', 6 => 'Grunge', 7 => 'Hip-Hop', 8 => 'Jazz', 9 => 'Metal', 10 => 'New Age', 11 => 'Oldies', 12 => 'Other',
13 => 'Pop', 14 => 'R&B', 15 => 'Rap', 16 => 'Reggae', 17 => 'Rock', 18 => 'Techno', 19 => 'Industrial', 20 => 'Alternative', 21 => 'Ska', 22 => 'Death Metal', 23 => 'Pranks', 24 => 'Soundtrack', 25 => 'Euro-Techno', 26 => 'Ambient', 27 => 'Trip-Hop', 28 => 'Vocal',
29 => 'Jazz+Funk', 30 => 'Fusion', 31 => 'Trance', 32 => 'Classical', 33 => 'Instrumental', 34 => 'Acid', 35 => 'House',
36 => 'Game', 37 => 'Sound Clip', 38 => 'Gospel', 39 => 'Noise', 40 => 'Alternative Rock', 41 => 'Bass', 42 => 'Soul', 43 => 'Punk', 44 => 'Space', 45 => 'Meditative', 46 => 'Instrumental Pop', 47 => 'Instrumental Rock', 48 => 'Ethnic',
49 => 'Gothic', 50 => 'Darkwave', 51 => 'Techno-Industrial', 52 => 'Electronic', 53 => 'Pop-Folk', 54 => 'Eurodance', 55 => 'Dream', 56 => 'Southern Rock', 57 => 'Comedy', 58 => 'Cult', 59 => 'Gangsta', 60 => 'Top 40', 61 => 'Christian Rap', 62 => 'Pop/Funk', 63 => 'Jungle', 64 => 'Native US', 65 => 'Cabaret', 66 => 'New Wave', 67 => 'Psychadelic', 68 => 'Rave', 69 => 'Showtunes', 70 => 'Trailer', 71 => 'Lo-Fi', 72 => 'Tribal', 73 => 'Acid Punk', 74 => 'Acid Jazz', 75 => 'Polka', 76 => 'Retro', 77 => 'Musical', 78 => 'Rock & Roll', 79 => 'Hard Rock', 80 => 'Folk', 81 => 'Folk-Rock', 82 => 'National Folk', 83 => 'Swing', 84 => 'Fast Fusion', 85 => 'Bebob', 86 => 'Latin',
87 => 'Revival', 88 => 'Celtic', 89 => 'Bluegrass', 90 => 'Avantgarde', 91 => 'Gothic Rock', 92 => 'Progressive Rock', 93 => 'Psychedelic Rock', 94 => 'Symphonic Rock', 95 => 'Slow Rock', 96 => 'Big Band', 97 => 'Chorus', 98 => 'Easy Listening', 99 => 'Acoustic',
100 => 'Humour', 101 => 'Speech', 102 => 'Chanson', 103 => 'Opera', 104 => 'Chamber Music', 105 => 'Sonata', 106 => 'Symphony', 107 => 'Booty Bass', 108 => 'Primus', 109 => 'Porn Groove', 110 => 'Satire', 111 => 'Slow Jam', 112 => 'Club',
113 => 'Tango', 114 => 'Samba', 115 => 'Folklore', 116 => 'Ballad', 117 => 'Power Ballad', 118 => 'Rhytmic Soul', 119 => 'Freestyle', 120 => 'Duet', 121 => 'Punk Rock', 122 => 'Drum Solo', 123 => 'Acapella', 124 => 'Euro-House',
125 => 'Dance Hall', 126 => 'Goa', 127 => 'Drum & Bass', 128 => 'Club-House', 129 => 'Hardcore', 130 => 'Terror', 131 => 'Indie', 132 => 'BritPop', 133 => 'Negerpunk', 134 => 'Polsk Punk', 135 => 'Beat', 136 => 'Christian Gangsta Rap', 137 => 'Heavy Metal', 138 => 'Black Metal', 139 => 'Crossover', 140 => 'Contemporary Christian', 141 => 'Christian Rock', 142 => 'Merengue', 143 => 'Salsa', 144 => 'Trash Metal', 145 => 'Anime', 146 => 'Jpop', 147 => 'Synthpop'
);
public function __construct() {
$this->info = '';
}
private function getId3() {
$handle = fopen($this->file, 'r');
$head = fread($handle,10);
$head = unpack("a3signature/c1version_major/c1version_minor/c1flags/Nsize", $head);
$result = array();
for ($i = 0; $i<5; $i++){
$tag = trim(fread($handle, 6));
if (!isset($this->tags[$tag])) continue;
$size = fread($handle, 2);
$size = unpack('n', $size); $size = $size[1]+2;
$value = fread($handle, $size);
$this->info[$this->tags[$tag]] = $value;
} fclose($handle);
}
public function load($file) {
$this->file = $file; $this->getId3(); }
}
$id3 = new Id3();
$id3->load('mp3.mp3');
print_R($id3->info);
now, when i read the value i get this:
Array
(
[genre] => ��Rock
[title] => ��Walk
[artist] => ��Foo Fighters
[album] => ��Wasting Light
)
what is wrong with my code?
based on your code, the $value needs to be trim or converted to UTF (I prefer converting to UTF).
I added function that will check which UTF is the mp3 and parse it correctly:
$value = $this->filter($value, $tag); // convert to UTF or else that is why you have weird chars
Here's a readable code.
<?php
class Id3 {
private $tags = array(
'TALB' => 'album',
'TCON' => 'genre',
'TENC' => 'encoder',
'TIT2' => 'title',
'TPE1' => 'artist',
'TPE2' => 'ensemble',
'TYER' => 'year',
'TCOM' => 'composer',
'TCOP' => 'copyright',
'TRCK' => 'track',
'WXXX' => 'url',
'COMM' => 'comment'
);
private $genre = array(
0 => 'Blues',
1 => 'Classic Rock',
2 => 'Country',
3 => 'Dance',
4 => 'Disco',
5 => 'Funk',
6 => 'Grunge',
7 => 'Hip-Hop',
8 => 'Jazz',
9 => 'Metal',
10 => 'New Age',
11 => 'Oldies',
12 => 'Other',
13 => 'Pop',
14 => 'R&B',
15 => 'Rap',
16 => 'Reggae',
17 => 'Rock',
18 => 'Techno',
19 => 'Industrial',
20 => 'Alternative',
21 => 'Ska',
22 => 'Death Metal',
23 => 'Pranks',
24 => 'Soundtrack',
25 => 'Euro-Techno',
26 => 'Ambient',
27 => 'Trip-Hop',
28 => 'Vocal',
29 => 'Jazz+Funk',
30 => 'Fusion',
31 => 'Trance',
32 => 'Classical',
33 => 'Instrumental',
34 => 'Acid',
35 => 'House',
36 => 'Game',
37 => 'Sound Clip',
38 => 'Gospel',
39 => 'Noise',
40 => 'Alternative Rock',
41 => 'Bass',
42 => 'Soul',
43 => 'Punk',
44 => 'Space',
45 => 'Meditative',
46 => 'Instrumental Pop',
47 => 'Instrumental Rock',
48 => 'Ethnic',
49 => 'Gothic',
50 => 'Darkwave',
51 => 'Techno-Industrial',
52 => 'Electronic',
53 => 'Pop-Folk',
54 => 'Eurodance',
55 => 'Dream',
56 => 'Southern Rock',
57 => 'Comedy',
58 => 'Cult',
59 => 'Gangsta',
60 => 'Top 40',
61 => 'Christian Rap',
62 => 'Pop/Funk',
63 => 'Jungle',
64 => 'Native US',
65 => 'Cabaret',
66 => 'New Wave',
67 => 'Psychadelic',
68 => 'Rave',
69 => 'Showtunes',
70 => 'Trailer',
71 => 'Lo-Fi',
72 => 'Tribal',
73 => 'Acid Punk',
74 => 'Acid Jazz',
75 => 'Polka',
76 => 'Retro',
77 => 'Musical',
78 => 'Rock & Roll',
79 => 'Hard Rock',
80 => 'Folk',
81 => 'Folk-Rock',
82 => 'National Folk',
83 => 'Swing',
84 => 'Fast Fusion',
85 => 'Bebob',
86 => 'Latin',
87 => 'Revival',
88 => 'Celtic',
89 => 'Bluegrass',
90 => 'Avantgarde',
91 => 'Gothic Rock',
92 => 'Progressive Rock',
93 => 'Psychedelic Rock',
94 => 'Symphonic Rock',
95 => 'Slow Rock',
96 => 'Big Band',
97 => 'Chorus',
98 => 'Easy Listening',
99 => 'Acoustic',
100 => 'Humour',
101 => 'Speech',
102 => 'Chanson',
103 => 'Opera',
104 => 'Chamber Music',
105 => 'Sonata',
106 => 'Symphony',
107 => 'Booty Bass',
108 => 'Primus',
109 => 'Porn Groove',
110 => 'Satire',
111 => 'Slow Jam',
112 => 'Club',
113 => 'Tango',
114 => 'Samba',
115 => 'Folklore',
116 => 'Ballad',
117 => 'Power Ballad',
118 => 'Rhytmic Soul',
119 => 'Freestyle',
120 => 'Duet',
121 => 'Punk Rock',
122 => 'Drum Solo',
123 => 'Acapella',
124 => 'Euro-House',
125 => 'Dance Hall',
126 => 'Goa',
127 => 'Drum & Bass',
128 => 'Club-House',
129 => 'Hardcore',
130 => 'Terror',
131 => 'Indie',
132 => 'BritPop',
133 => 'Negerpunk',
134 => 'Polsk Punk',
135 => 'Beat',
136 => 'Christian Gangsta Rap',
137 => 'Heavy Metal',
138 => 'Black Metal',
139 => 'Crossover',
140 => 'Contemporary Christian',
141 => 'Christian Rock',
142 => 'Merengue',
143 => 'Salsa',
144 => 'Trash Metal',
145 => 'Anime',
146 => 'Jpop',
147 => 'Synthpop'
);
private $file;
private $info;
public function __construct() {
$this->info = array(
'genre' => 'unknow',
'title' => 'unknow',
'artist' => 'unknow',
'album' => 'unknow',
);
}
private function filter($tag, $type) {
if ($type == 'COMM') {
$tag = substr($tag, 0, 3) . substr($tag, 10);
}
if(ord($tag[2]) == 0) {
return iconv('UTF-8', 'ISO-8859-1', substr($tag, 3));
}
elseif(ord($tag[2]) == 1) {
return iconv('UTF-16LE', 'UTF-8', substr($tag, 5));
}
elseif(ord($tag[2]) == 2) {
return iconv('UTF-16BE', 'UTF-8', substr($tag, 5));
}
elseif(ord($tag[2]) == 3) {
return substr($tag, 3);
}
return false;
}
private function getId3() {
$handle = fopen($this->file, 'rb');
$head = fread($handle, 10);
$head = unpack("a3signature/c1version_major/c1version_minor/c1flags/Nsize", $head);
if (!$head['signature'] == 'ID3') {
fclose($handle);
return false;
}
$result = array();
for ($i = 0; $i < 22; $i++) { //22 most popular tags, you had 5 therefore only the first five.
$tag = rtrim(fread($handle, 6));
if (!isset($this->tags[$tag])) {
continue;
}
$size = fread($handle, 2);
$size = unpack('n', $size);
$size = $size[1]+2;
$value = fread($handle, $size);
$value = $this->filter($value, $tag); // convert to UTF or else that is why you have weird chars
$this->info[$this->tags[$tag]] = $value;
}
fclose($handle);
}
public function load($file) {
$this->file = $file;
$this->getId3();
}
public function getInfo() {
return $this->info;
}
}
$id3 = new Id3();
$id3->load('mp3.mp3');
print_R($id3->getInfo());
To get more information about tags, read this wiki article: http://en.wikipedia.org/wiki/ID3