如何在子foreach循环中自动递增?

时间:2016-03-11 07:36:51

标签: php arrays

以下是示例代码:

#include <boost/bimap.hpp>
#include <boost/assign.hpp>

//declare the type of bimap we want
typedef boost::bimap<int, std::string> bimapType;
//init our bimap
bimapType bimap = boost::assign::list_of< bimapType::relation >
( 1, "one"   )
( 2, "two"   )
( 3, "three" );

//test if everything works
int main(int argc, char **argv)
{
    std::cout << bimap.left.find(1)->second << std::endl;
    std::cout << bimap.left.find(2)->second << std::endl;
    std::cout << bimap.left.find(3)->second << std::endl;
    std::cout << bimap.right.find("one")->second << std::endl;
    std::cout << bimap.right.find("two")->second << std::endl;
    std::cout << bimap.right.find("three")->second << std::endl;

    /* Output:
     * one
     * two
     * three
     * 1
     * 2
     * 3
     */
}

我不知道如何解释它,但我想要的是在sub foreach循环中添加自动增量,如下所示:

    <?php 
    $arr = array(
    array(
        'company' => array(
            'code' => 'ccd1',
            'name' => 'cnm1'
        ) ,
        'products' => array(
            array(
                'code' => 'pcd1',
                'name' => 'pnm1'
            ) ,
            array(
                'code' => 'pcd2',
                'name' => 'pnm2'
            )
        )
    ) ,
    array(
        'company' => array(
            'code' => 'ccd2',
            'name' => 'cnm2'
        ) ,
        'products' => array(
            array(
                'code' => 'pcd1',
                'name' => 'pnm1'
            ) ,
            array(
                'code' => 'pcd2',
                'name' => 'pnm2'
            ) ,
            array(
                'code' => 'pcd3',
                'name' => 'pnm3'
            )
        )
    )
);


echo "<pre>";   print_r($arr);      echo "</pre>";
$AI = 1;
foreach($arr as $value){

    $total_products =  count($value['products']);
    echo $AI++.".{$value['company']['name']} ({$total_products})<br />";

    foreach($value['products'] as $value2){
        echo "&nbsp;&nbsp;&nbsp;&nbsp;".$value2['name']."<br />";
    }
}

5 个答案:

答案 0 :(得分:4)

通常您只需使用_(L.error.notfound.title,"the stuff")键加一。作为另一种选择,如果这只是用于演示,只需使用有序列表:

foreach

它会相应地对这些项目进行编号。无需添加。另外,您可以使用CSS来设置列表样式,

答案 1 :(得分:1)

您可以访问foreach的键/索引:

$flipped = array_flip($categories);
foreach ( $input_categories as $v ) {

    echo $flipped[$v];      echo "<br/>";
}

答案 2 :(得分:0)

你走了。

$arr = array(array('company'=>array('code'=>'ccd1', 'name'=>'cnm1'), 'products'=>array(array('code'=>'pcd1', 'name'=>'pnm1'), array('code'=>'pcd2', 'name'=>'pnm2'))), array('company'=>array('code'=>'ccd2', 'name'=>'cnm2'), 'products'=>array(array('code'=>'pcd1', 'name'=>'pnm1'), array('code'=>'pcd2', 'name'=>'pnm2'), array('code'=>'pcd3', 'name'=>'pnm3'))));

echo "<pre>";
print_r($arr);
echo "</pre>";

$AI = 1;
foreach($arr as $value)
{
  $total_products =  count($value['products']);
  echo $AI++.".{$value['company']['name']} ({$total_products})<br />";
  $k = 0;

  foreach($value['products'] as $value2)
  {
    echo "&nbsp;&nbsp;&nbsp;&nbsp;".$k++.". ".$value2['name']."<br />";
  }
}

答案 3 :(得分:0)

这对我也很有用:

foreach($value['products'] as $key2=>$value2){
    $AI2 = $key2+1;
echo "&nbsp;&nbsp;&nbsp;&nbsp;".$AI2.".{$value2['name']}<br />";
}

答案 4 :(得分:0)

你可以试试这个:

foreach($arr as $value){
    $total_products =  count($value['products']);
    echo $AI++.".{$value['company']['name']} ({$total_products})<br />";
    $sub=1;
    foreach($value['products'] as $value2){
        echo "&nbsp;&nbsp;&nbsp;&nbsp;".$sub++.'.'.$value2['name']."<br />";
    }
}