【发布时间】:2015-02-28 01:19:04
【问题描述】:
越来越接近我的问题的解决方案......但现在我又被卡住了。
我正在尝试返回链接到帖子的所有音频附件。每个帖子都有一个 .ogg 和一个 .mp3 文件。每个文件都是一个 Wordpress 对象。因此,为 .ogg 和 .mp3 文件分离对象。所以我返回他们的'post_parent'值并用它来链接它们。我使用这个 'post_parent' 作为我数组中的键。它有效,但不像我想要的那样。
这是 $attachments(不完整,我删除了不重要的键,用于 foreach 循环)。您可以看到每个文件都是独立的,因此我必须自己将它们链接到 [post_parent],如您所见,有 4 个文件,但有 2 个不同的轨道。 ID 不同,但 [post_parent] 相同:
[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
)
这是我目前得到的,其实是对的……
[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
)
)
...但我的代码(在底部)不能这样做:
[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
)
)
这是我使用的代码。这很奇怪,我知道。我正在学习。
我的问题是,如您所见,我使用数字 0 和 1,我只是将它们硬编码进去。但如果有 4 个文件在一个发布(如我上面的示例)。第二个找到的文件之后的文件将替换 $playlist_all[$parent_id][1]。所以 [2] 和 [3] 不会被创建。顺便说一下,我完全知道为什么,但我不知道如何让这些东西检查数组中是否有一个键,如果有,就增加它。我知道 for 循环和增量的东西......只是不知道它会如何工作......
再次,我知道我哪里出错了,但这是我能想到的唯一解决方案。我只是检查是否有 [0],如果没有则创建它。如果有 [0] 我创建 [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;
}
}
}
我希望它很清楚,它是杂乱的代码,但我让它工作了。 50%。
提前致谢。
【问题讨论】:
-
你能给我们你正在使用的$attachments的数组吗?
-
$playlist_all应该做什么? -
我想您可能正在寻找像
$array[] = $value这样简单的东西。这只会在新的$array密钥中填充$value。 -
@Demodave 我会将它添加到 OP 中。 Bono 我用它来用 Soundmanager2 或 jPlayer 播放文件。现在每个帖子都有自己的文件播放器,但他们想要一个独立的播放器。
-
@Bono 这里有一个并发症。不需要每次都创建新索引。例如,如果他只是解析一个 ogg 文件,他会创建索引 0。因此,如果他接下来得到一个 mp3,他会坚持使用索引 0。直到他得到第二个 ogg 或 mp3,他才需要添加一个新的索引。