getting closer to the solution of my problem... but now I am stuck again.
I am trying return all audio attachments linked to a post. Each post has an .ogg and a .mp3 file. Each file is an Wordpress object. So separate objects for the .ogg and .mp3 files. So I return their 'post_parent' value and use this to link them. I use this 'post_parent' as a key in my array. It works, but not like I want it to.
This is $attachments (not complete, I removed the non important keys, used in the foreach loop). You can see each file is separate so I have to link them myself to the [post_parent], as you can see, there are 4 files, but 2 different tracks. ID's are different, but the [post_parent] is the same:
[13] => WP_Post Object (
[ID] => 18619
[post_title] => IanEwing - Beauty
[post_parent] => 18612
[guid] => http://www.moovmnt.com/wp-content/uploads/2015/02/IanEwing-Beauty.ogg
[post_type] => attachment
[post_mime_type] => audio/ogg
)
[14] => WP_Post Object (
[ID] => 18618
[post_title] => IanEwing - Beauty
[post_parent] => 18612
[guid] => http://www.moovmnt.com/wp-content/uploads/2015/02/IanEwing-Beauty.mp3
[post_type] => attachment
[post_mime_type] => audio/mpeg
)
[15] => WP_Post Object (
[ID] => 18617
[post_title] => C Y G N - S U P E R N O V A
[post_parent] => 18612
[guid] => http://www.moovmnt.com/wp-content/uploads/2015/02/C-Y-G-N-S-U-P-E-R-N-O-V-A.ogg
[post_type] => attachment
[post_mime_type] => audio/ogg
)
[16] => WP_Post Object (
[ID] => 18616
[post_title] => C Y G N - S U P E R N O V A
[post_parent] => 18612
[guid] => http://www.moovmnt.com/wp-content/uploads/2015/02/C-Y-G-N-S-U-P-E-R-N-O-V-A.mp3
[post_type] => attachment
[post_mime_type] => audio/mpeg
)
This is what I currently get, it is actually correct...
[18653] => Array (
[0] => Array (
[mp3] => 1.mp3
[ogg] => 1.ogg
)
)
[18612] => Array (
[0] => Array (
[ogg] => 2.ogg
[mp3] => 2.mp3
)
[1] => Array (
[ogg] => 3.ogg
[mp3] => 3.mp3
)
)
[18605] => Array (
[0] => Array (
[ogg] => 4.ogg
[mp3] => 4.mp3
)
)
[18592] => Array (
[0] => Array (
[ogg] => 5.ogg
[mp3] => 5.mp3
)
[1] => Array (
[ogg] => 6.ogg
[mp3] => 7.mp3
)
)
... but my code (at the bottom) can't do this:
[18612] => Array (
[0] => Array (
[ogg] => 12.ogg
[mp3] => 12.mp3
)
[1] => Array (
[ogg] => 13.ogg
[mp3] => 13.mp3
)
[2] => Array (
[ogg] => 14.ogg
[mp3] => 14.mp3
)
[3] => Array (
[ogg] => 15.ogg
[mp3] => 15.mp3
)
)
This is the code I use. It's weird, I know. I'm learning.
My problem is, as you can see, I use the numbers 0 and 1, I just hard coded them in. But it won't work if there are let's say, 4 files in a post (like my sample above). The files after the second found file will replace the $playlist_all[$parent_id][1]. So the [2] and [3] will not be created. And I exactly know why by the way, but I don't know how to make this stuff check if there is an key inside the array, and if so, increment it. I know about for loops and increment stuff... just not sure how it would work with this...
Again, I know where I am going wrong, but this is the only solution I can think of. I just check if there is a [0], if not create it. If there is a [0] I create the [1].
foreach ( $attachments as $attachment ) {
$parent_id = $attachment->post_parent;
$title = $attachment->post_title;
$type = $attachment->post_mime_type;
if ($type == 'audio/ogg') {
if (!$playlist_all[$parent_id][0]['ogg']) {
$playlist_all[$parent_id][0]['ogg'] = $attachment->guid;
} else {
$playlist_all[$parent_id][1]['ogg'] = $attachment->guid;
}
} elseif ($type == 'audio/mpeg') {
if (!$playlist_all[$parent_id][0]['mp3']) {
$playlist_all[$parent_id][0]['mp3'] = $attachment->guid;
} else {
$playlist_all[$parent_id][1]['mp3'] = $attachment->guid;
}
}
}
I hope it's clear, it's messy code, but I got it to work. Well for 50%.
Thanks in advance.
Not tested (hard to do without the data), but something like this should work
foreach($attachments as $attachment) {
$parent_id = $attachment->post_parent;
$title = $attachment->post_title;
$type = $attachment->post_mime_type;
$ext = $type === 'audio/ogg' ? 'ogg' : 'mp3';
$arr = $playlist_all[$parent_id];
for ($i=0; $i<=count($arr); $i++) {
if (!(array_key_exists($i, $arr) && array_key_exists($ext, $arr[$i]))) {
$arr[$i][$ext] = $attachment->guid;
break;
}
}
}
It adds 1 to the number of indices in the array, then check every one of them, the last number not existing in the array will make sure the value is written, but if there is an index where the file extension doesn't already exists it's written in there instead, and then breaking the loop.
Run second foreach
loop inside of first one, and debug content of $playlist
. I'm quite sure that you can do that on a database level.
foreach ($attachments as $attachment) {
$parent_id = $attachment->post_parent;
$title = $attachment->post_title;
$type = $attachment->post_mime_type;
foreach ($playlist_all[$parent_id] as $playlist) {
if ($type == 'audio/ogg') {
// ...
} elseif ($type == 'audio/mpeg') {
// ...
}
}
}