如何在数组声明中实现循环

发布于 2025-01-05 09:45:36 字数 1056 浏览 0 评论 0原文

我有下面的数组 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>
    '
));

I have the below array em_events(array( and need to insert the below foreach to repeat the $delete link. How do in put a foreach into an array? Not sure how best to describe this.

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>
    '
));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心意如水 2025-01-12 09:45:36

我想你想要的是这样的:

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>'
     ));
}

I think what you want is this:

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>'
     ));
}
疯狂的代价 2025-01-12 09:45:36

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

$li = <<<HTML
<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>
HTML;

em_events([
    'owner' => 4,
    'limit' => 10,
    'scope' => 'all',
    'format' => implode(
        "\n",
        array_map(
            fn($event) => sprintf(
                $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
        )
    )
]);

Build your HTML string in the array declaration using array_map(). In array_map()'s callback, use sprintf() to establish a placeholder to be replaced by your href value generator.

$li = <<<HTML
<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>
HTML;

em_events([
    'owner' => 4,
    'limit' => 10,
    'scope' => 'all',
    'format' => implode(
        "\n",
        array_map(
            fn($event) => sprintf(
                $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
        )
    )
]);
吃兔兔 2025-01-12 09:45:36

这个怎么样:

$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,
 ));

What about this:

$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,
 ));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文