douangzhao4108 2013-07-19 05:21
浏览 52
已采纳

将foreach循环中的数组连接到嵌套数组的单个变量中

I'm trying to get a list of different image sizes in Wordpress. I think this is more of a php question though so I'm asking it here instead of on the wordpress forum.

Here is my code:

function ajax_get_latest_posts(){   
   $attachments = get_children(array('post_parent' => $post->ID,
                    'post_status' => 'inherit',
                    'post_type' => 'attachment',
                    'post_mime_type' => 'image',
                    'order' => 'ASC',
                    'orderby' => 'menu_order ID'));

   foreach($attachments as $att_id => $attachment) {
      $large_bg_url = wp_get_attachment_image_src($attachment->ID, 'background-large');
      $medium_bg_url = wp_get_attachment_image_src($attachment->ID, 'background-medium');
      $small_bg_url = wp_get_attachment_image_src($attachment->ID, 'background-small');
      $imagesizes .= array('background_large' => $large_bg_url[0], 'background_medium' => $medium_bg_url[0], 'background_small' => $small_bg_url[0]);
   }

   $allimagesizes = array($imagesizes);

   return $allimagesizes;
}

The above code does not work but hopefully shows what I'm trying to achieve. The problem is the concatenation of $imagesizes is not right. Below is the output I actually need to achieve but I cannot seem to figure out how to get this output from the foreach loop.

$allimagesizes = array(
   array(
      'background_large' => 'http://mydomain.com.au/wp-content/uploads/facade.jpg', 
      'background_medium' => 'http://mydomain.com.au/wp-content/uploads/facade-1366x807.jpg', 
      'background_small' => 'http://mydomain.com.au/wp-content/uploads/facade-1024x605.jpg '
   ),
   array(
      'background_large' => 'http://mydomain.com.au/wp-content/uploads/facade-trees.jpg', 
      'background_medium' => 'http://mydomain.com.au/wp-content/uploads/facade-trees-1366x818.jpg', 
      'background_small' => 'http://mydomain.com.au/wp-content/uploads/facade-trees-1024x613.jpg '
   )    
 );
  • 写回答

2条回答 默认 最新

  • duanmao1919 2013-07-19 05:47
    关注

    You almost had it.

    function ajax_get_latest_posts(){   
       $attachments = get_children(array('post_parent' => $post->ID,
                        'post_status' => 'inherit',
                        'post_type' => 'attachment',
                        'post_mime_type' => 'image',
                        'order' => 'ASC',
                        'orderby' => 'menu_order ID'));
    
       $allimagesizes = array();
       foreach($attachments as $att_id => $attachment) {
          $large_bg_url = wp_get_attachment_image_src($attachment->ID, 'background-large');
          $medium_bg_url = wp_get_attachment_image_src($attachment->ID, 'background-medium');
          $small_bg_url = wp_get_attachment_image_src($attachment->ID, 'background-small');
          $imagesizes = array('background_large' => $large_bg_url[0], 'background_medium' => $medium_bg_url[0], 'background_small' => $small_bg_url[0]);
          $allimagesizes[] = $imagesizes;
       }
       return $allimagesizes;
    }
    

    Should work.

    First off we defined the array before the foreach.

    Remove the concatenation operator from the imagesizes assignment.

    Next we are pushing the array created in the loop into the $allimagessizes array inside the loop.

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

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配