dsk95913 2015-02-27 16:42
浏览 19
已采纳

推入阵列和foreach循环很难

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.

  • 写回答

2条回答 默认 最新

  • donkey199024 2015-02-27 16:57
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥35 引用csv数据文件(4列1800行),通过高斯-赛德尔法拟合曲线,在选取(每五十点取1点)数据,求该数据点的曲率中心。
  • ¥20 程序只发送0X01,串口助手显示不正确,配置看了没有问题115200-8-1-no,如何解决?
  • ¥15 Google speech command 数据集获取
  • ¥15 vue3+element-plus页面崩溃
  • ¥15 像这种代码要怎么跑起来?
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧