【发布时间】:2012-10-28 21:45:03
【问题描述】:
首先,如果我的初级 PHP 技能迫使我询问一些可能是基本的或微不足道的问题,但我搜索了该站点但没有结果(可能是因为我不知道此类问题的技术术语),请原谅。 .)
我对每个循环都有一个嵌套,但基于简单变量 ($e),我需要使用整个循环(2 个嵌套部分)或只使用一个(内部)。
$a = array(); //some array ...
$c = array(); //some other array ...
$e = 'yes' // or 'no'
if ($a) {
foreach ($a as $b) {
$a_1[] = 'something_quite_long'; //that would affects $c if used
foreach ( $c as $d ) {
//do_something_very long...
}
}
}
所以现在我已经通过使用 switch $e: 和 case 解决了这个问题 - 但这比我的代码长度重复更多,因为我所做的是一个案例作为整个嵌套循环,另一个案例- 唯一的内循环(这是loooong ..)
我确信在这种情况下可以使用一些忍者技术,而无需简单地将整个代码复制并粘贴到“case”中 - 但我不知道如何。
有人吗?
注意:我知道我大概可以使用外部函数,但问题是在很长的第二个(内部)循环中 - 有一个或两个小情况,其中口头输出 (HTML) 会略有变化..
编辑我 - 由于 cmets 不明确,这里是代码(循环大大减少 - 实际循环更长) - 尽管与问题无关。代码与wordpress相关..
function o99_get_image_size_links($title='',$single,$recieved_id) {
/* If not viewing an image attachment page, return. */
/* Set up an empty array for the links. */
$links = array();
/* Get the intermediate image sizes and add the full size to the array. */
$sizes = get_intermediate_image_sizes();
$sizes[] = 'full';
global $post;
switch ($single) {
case 'FALSE':
// this will do to all images attached to a post ..
$attachments = get_children( array('post_parent' => $post->ID, 'post_status' => 'inherit', 'post_type' => 'attachment','post_mime_type' => $mime, 'order' => 'ASC', 'orderby' => 'menu_order ID', 'posts_per_page'=>$limit) );
/* Loop through each of the image sizes. */
if ($attachments) {
foreach ($attachments as $att) {
$links[] = '<br>' . get_the_title($att->ID) .' || '. basename ( get_attached_file( $att->ID ) ).' || '. $att->post_name .' || ' /*. wp_get_attachment_image( $att->ID, 'tooltip' ).' || ' .'<br/>'*/ ; // k99 add
if ($title) {
$links[] = '<br><b>' . $title . '</b></br>';
}
foreach ( $sizes as $size ) {
/* Get the image source, width, height, and whether it's intermediate. */
$image = wp_get_attachment_image_src( $att->ID, $size );
/* Add the link to the array if there's an image and if $is_intermediate (4th array value) is true or full size. */
if ( !empty( $image ) && ( true == $image[3] || 'full' == $size ) )
$links[] = "</br><a class='image-size-link_{$image[1]}×{$image[2]}' href='{$image[0]}'>{$image[1]} × {$image[2]}</a>";
}
}
}
break;
//case for single image ..
case 'TRUE':
$links[] = '<br>' . get_the_title($recieved_id) .' || '. basename ( get_attached_file( $recieved_id ) ).' || '. $att->post_name .' || ' /*. wp_get_attachment_image( $att->ID, 'tooltip' ).' || ' .'<br/>'*/ ; // k99 add
if ($title) {
$links[] = '<br><b>' . $title . '</b></br>';
}
foreach ( $sizes as $size ) {
/* Get the image source, width, height, and whether it's intermediate. */
$image = wp_get_attachment_image_src( $recieved_id, $size );
/* Add the link to the array if there's an image and if $is_intermediate (4th array value) is true or full size. */
if ( !empty( $image ) && ( true == $image[3] || 'full' == $size ) )
$links[] = "</br><a class='image-size-link_{$image[1]}×{$image[2]}' href='{$image[0]}'>{$image[1]} × {$image[2]}</a>";
}
break;
}
/* Join the links in a string and return. */
return join( '<span class="sep"> /</span> ', $links );
}
【问题讨论】:
-
在您的代码中看不到任何
switch语句??并且从您的代码中不清楚您要实现的目标 -
@Baba nop - 因为开关只会复制循环。这就是为什么我只是口头写了它(整个代码只是一个例子)。如有必要,我可以添加它..
-
当您之前将 $d 定义为某种“是/否”标志时,您将无法使用
$c as $d。您的标志将被覆盖。 -
@SamuelCook - 对不起,我的错,现在修好了..
-
请添加预期的输入和输出..这样可以清楚您想要达到的目标
标签: php loops nested switch-statement case