如何将计数附加到 php 中的变量

How to append count to variables in php

我刚刚开始学习 PHP 并且为了解决这个问题,我正在尝试修改现有代码以匹配我正在尝试做的事情。我有多个变量,我试图在部分使用计数器的同时定义所有变量。我的问题是将计数附加到我单独定义的变量中。另外,我确信有一种更简洁的方法可以做到这一点(比如循环或嵌套数组),我只是看不出如何做。

<?php                   
$pageId0 = opt('first_tab_page');
    $post0 = get_post($pageId0);  
    $content0 = apply_filters('the_content', $post0->post_content); 
    $icon0 = wp_get_attachment_image_src( get_post_thumbnail_id( $post0->ID ), 'full' ); 

$pageId1 = opt('second_tab_page');
    $post1 = get_post($pageId1);            
    $content1 = apply_filters('the_content', $post1->post_content); 
    $icon1 = wp_get_attachment_image_src( get_post_thumbnail_id( $post1->ID ), 'full' ); 

$pageId2 = opt('third_tab_page');
    $post2 = get_post($pageId2);        
    $content2 = apply_filters('the_content', $post2->post_content); 
    $icon2 = wp_get_attachment_image_src( get_post_thumbnail_id( $post2->ID ), 'full' );        
?>

<div>
<?php echo $icon0[0]; ?>
</div>

<div>
<?php echo $icon1[0]; ?>
</div>

<div>
<?php echo $icon2[0]; ?>
</div>

<?php 
    $tab_position = opt('tab_position');
    if ($tab_position == '' || count($tab_position) != 3) {
        $tab_position = array(0, 1, 2);
    }
    for($i=0; $i < count($tab_position); $i++)
        {
        $selected = '';
        if (opt('default_selected_tab') == $tab_position[$i]){
                $selected = 'class="selected"';
        }
?>  
<a <?php echo $selected; ?> href="#tab<?php echo $tab_position[$i];?>">
<?php echo $content//should be 0 to start; ?>
</a>
<?php } ?>

我不确定你想做什么,因为你的问题对我来说不是很清楚。
但我认为你是在回应 $content 在你的最后一行代码中。如果是,只需将所有变量 $content1$content2$content3 放在一个数组中如下所示:

$contents = array($content1, $content2, $content3);

然后使用foreach循环,像这样遍历这个数组

$count = 0;
foreach($contents as $content) {
    echo $content.$count;
    $count++;   
}

试试看是否适合你。

我想除了(嵌套)数组之外别无他法。另外我认为数组是一个可以接受的解决方案。

foreach ($tabs as $tab) {
    $tab['content'] = 'somecontent';
    $tab['counter'] = 'counter';
}

foreach ($tabs as $tab) {
    $tab = [
      "content" => "content",
      "counter" => "counter"];
}

这是一个关于使用数组的简短示例。在代码中评论

// Storing tab names to an array, just add more tabs if required 
// and details for all those will be loaded in the following arrays
$tabs = array('first_tab_page', 'second_tab_page', 'third_tab_page');

//declare arrays to store (not required, but better practice)
$pageids = array();
$posts = array();
$content = array();
$icons = array();
//Iterate through the tabs array
foreach ($tabs as $tab){
    //Store page id and post in a variable, we require it
    $pageid = opt($tab);
    $post = get_post($pageid);
    // Store pageid in pageids. note the [] this means it will store pageid at next index
    //also store other items
    $pageids[] = $pageid;
    $posts[] = $post;
    $contents[] = apply_filters('the_content', $post->post_content);
    $icons[] = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
}

现在 $icons[0] 将包含第一个图标 $icons[1] 第二个图标,依此类推。同样的事情适用于其他变量。

注意:此代码只是在此处输入,未经过检查。如果有任何语法错误,请修复。另请注意,这是一种方式,还有更多方式。

但我建议将每个页面的数据放在一起。

编辑:这里是你如何把东西放在一起(只显示相关部分)

$tabs = array('first_tab_page', 'second_tab_page', 'third_tab_page');
$pages = array();
foreach ($tabs as $tab){
    $pageid = opt($tab);
    $post = get_post($pageid);
    $content = apply_filters('the_content', $post->post_content);
    $icon = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
    //Store everything to $pages array
    $pages[] = array('pageid' => $pageid, 'post' => $post, 'content' => $content, 'icon', $icon);
}

foreach ($pages as $page){
?>

<div>
<?php echo $page['icon'][0]; ?>
</div>

<?php } ?>


foreach ($pages as $page){
?>

<a <?php echo $selected; ?> href="#tab<?php echo $tab_position[$i];?>">
<?php echo $page['content']; //echo the content of page ?>
</a>

<?php } ?>

你也可以使用索引代替 foreach 来显示

<a <?php echo $selected; ?> href="#tab<?php echo $tab_position[$i];?>">
<?php echo $pages[0]['content']; //echo the content of the first page ?>
</a>

<a <?php echo $selected; ?> href="#tab<?php echo $tab_position[$i];?>">
<?php echo $pages[1]['content']; //echo the content of the second page ?>
</a>