循环遍历数组并抓住键

时间:2010-09-06 22:04:59

标签: php multidimensional-array

Array ( [Screens] => 
   Array ( [0] => 
      Array ( [ SPKRS ] => 
          Array ( [price] => 455 
                  [quantity] => 3
                  [image] =>

                )
          )      
     ) [Software] => Array 
           ( [0] => Array 
             ( [Pricing] => 
                 Array ( [price] => 2 
                         [quantity] => 2 
                         [image] => SOFTWARE.png 

               [ALDELO FOR RESTAURANTS] => Array 
                       ( [price] => 535 
                         [quantity] => 1 
                         [image] =>

) ) ) [Networking] => Array
 ....

数组是$ products

<?php foreach ($products as $product): ?> 
   <div class="box-active">
    <div class="line">
    <div class="text left">
    <p><?php print key($product); ?></p>

它为所有三个

打印0

1 个答案:

答案 0 :(得分:3)

试试这个:

<?php foreach ($products as $key => $product): ?> 
   <div class="box-active">
    <div class="line">
    <div class="text left">
    <p><?php print $key; ?></p>

请参阅manual page on foreach