在数组中使用foreach

问题描述 投票:0回答:3

我有下面的数组

em_events(array(
,需要插入下面的foreach来重复
$delete
链接。如何将 foreach 放入数组中?不知道如何最好地描述这一点。

foreach ($EM_Events as $event) {
    $delete = esc_url(add_query_arg(array('action'=>'event_delete', 'event_id'=>$event->event_id, '_wpnonce'=> wp_create_nonce('event_delete_'.$event->event_id))));
}


    em_events(array(
    'owner'=>4,
    'limit'=>10,
    'scope'=>'all',
    'format'=>'

<li class="even portfolio-entry">
    <div class="entry-thumb standard">
        <a class="thumblink" href="'.$delete.'"><span class="thumbnail">#_EVENTIMAGE{220,220}</span><span class="extra"><strong>Edit Event</strong></span></a>
    </div>

    <div class="entry-title">
        <a href="#_EVENTURL" title="#_EVENTURL" rel="bookmark"><span style="color:#00b1d9;">PREVIEW EVENT POST</span><span>#F #j, #Y</span>#_NAME</a>
    </div>
</li>   

    '
     ));
php arrays foreach
3个回答
0
投票

我想你想要的是这样的:

foreach ($EM_Events as $event) {
    $delete = esc_url(add_query_arg(array('action'=>'event_delete', 'event_id'=>$event->event_id, '_wpnonce'=> wp_create_nonce('event_delete_'.$event->event_id))));



    em_events(array(
    'owner'=>4,
    'limit'=>10,
    'scope'=>'all',
    'format'=>'

  <li class="even portfolio-entry">
      <div class="entry-thumb standard">
          <a class="thumblink" href="'.$delete.'"><span class="thumbnail">#_EVENTIMAGE{220,220}</span><span class="extra"><strong>Edit Event</strong></span></a>
      </div>

      <div class="entry-title">
          <a href="#_EVENTURL" title="#_EVENTURL" rel="bookmark"><span style="color:#00b1d9;">PREVIEW EVENT POST</span><span>#F #j, #Y</span>#_NAME</a>
      </div>
  </li>'
     ));
}

0
投票

使用

array_map()
在数组声明中构建 HTML 字符串。 在
array_map()
的回调中,使用
sprintf()
建立一个占位符,以替换您的 href 值生成器。

em_events([
    'owner' => 4,
    'limit' => 10,
    'scope' => 'all',
    'format' => implode(
        "\n",
        array_map(
            fn($event) => sprintf(
                '<li class="even portfolio-entry">
                   <div class="entry-thumb standard">
                     <a class="thumblink" href="%s">
                       <span class="thumbnail">#_EVENTIMAGE{220,220}</span>
                       <span class="extra"><strong>Edit Event</strong></span>
                     </a>
                   </div>
                   <div class="entry-title">
                     <a href="#_EVENTURL" title="#_EVENTURL" rel="bookmark">
                       <span style="color:#00b1d9;">PREVIEW EVENT POST</span>
                       <span>#F #j, #Y</span>
                       #_NAME
                     </a>
                   </div>
                 </li>',
                esc_url(
                    add_query_arg([
                        'action' => 'event_delete',
                        'event_id' => $event->event_id,
                        '_wpnonce' =>  wp_create_nonce('event_delete_' . $event->event_id)
                    ])
                )
            ),
            $EM_Events
        )
    )
]);

-1
投票

这个怎么样:

$format=NULL;
foreach ($EM_Events as $event) {
            $delete = esc_url(add_query_arg(array('action'=>'event_delete', 'event_id'=>$event->event_id, '_wpnonce'=> wp_create_nonce('event_delete_'.$event->event_id))));

$format=$format.
'<li class="even portfolio-entry">
    <div class="entry-thumb standard">
        <a class="thumblink" href="'.$delete.'"><span class="thumbnail">#_EVENTIMAGE{220,220}</span><span class="extra"><strong>Edit Event</strong></span></a>
    </div>

    <div class="entry-title">
        <a href="#_EVENTURL" title="#_EVENTURL" rel="bookmark"><span style="color:#00b1d9;">PREVIEW EVENT POST</span><span>#F #j, #Y</span>#_NAME</a>
    </div>
</li>';   
}

em_events(array(
'owner'=>4,
'limit'=>10,
'scope'=>'all',
'format'=>$format,
 ));
© www.soinside.com 2019 - 2024. All rights reserved.