2 foreach。因为' n'水平,打破新的ul

时间:2015-11-02 10:19:53

标签: php foreach

我对php和api调用很陌生,我很难理解如何在列表到达' n'水平?我理解简单的分解标记,如下所示:Break and Create new after 10 values from foreach loop php

但我对我的实现有点困惑,因为调用我的api需要两对foreach嵌套。

这就是我现在所得到的:

enter image description here

这就是我应该期待的:

enter image description here

<ul class="menu" id="nav">
  <li>Shop By Categories</li>
  <?php 
    $resultListAllCategories = get_decoded_info ( $SERVER_URL, $PORT, 'tos-product/user/category/listAll', $fields );
    foreach ( $resultListAllCategories as $key => $eachDetail ) {
        foreach ( $eachDetail as $key => $eachData ) {
            if(! isset($eachData['parentCategoryId'])){
                $x = $eachData['catId'];?>
    <li><a href="#"><?php echo $eachData['catName'];?></a>
      <div class="megadrop">
        <div class="col">
<?php 
    $break_after = 2;

    $counter = 0;

    foreach ( $resultListAllCategories as $key => $eachDetail ) {
        if ($counter % $break_after == 0) {
            echo '<ul>';
        }
        foreach ( $eachDetail as $key => $eachData ) {
            if(isset($eachData['parentCategoryId'])){
                $y = $eachData['parentCategoryId'];
                if($y == $x) { 
                    echo '<li>'.$eachData['catName'].'</li>';
                    if ($counter % $break_after == ($break_after-1)) {
                        echo '</ul>';
                    }
                    ++$counter;
                }
             }
         }
    }

?>
        </div>
    </div>
    </li>
    <?php }}}?>
  </ul>

$ resultsListAllCategories

Array
(
    [categories] => Array
        (
            [0] => Array
                (
                    [catId] => ROC-CAT-1
                    [catName] => Mobile Phones
                    [projectId] => 8
                    [catStatus] => 1
                    [visible] => 1
                    [modifiedDate] => 1443467703
                    [createDate] => 1443467703
                )

            [1] => Array
                (
                    [catId] => ROC-CAT-10
                    [catName] => Lifestyle
                    [projectId] => 8
                    [catStatus] => 1
                    [visible] => 1
                    [modifiedDate] => 1443520324
                    [createDate] => 1443520290
                )

            [2] => Array
                (
                    [catId] => ROC-CAT-11
                    [catName] => Time sensitive
                    [parentCategoryId] => ROC-CAT-10
                    [projectId] => 8
                    [catStatus] => 1
                    [visible] => 1
                    [modifiedDate] => 1443520402
                    [createDate] => 1443520402
                )

            [3] => Array
                (
                    [catId] => ROC-CAT-12
                    [catName] => Media users
                    [parentCategoryId] => ROC-CAT-10
                    [projectId] => 8
                    [catStatus] => 1
                    [visible] => 1
                    [modifiedDate] => 1443520415
                    [createDate] => 1443520415
                )

            [4] => Array
                (
                    [catId] => ROC-CAT-13
                    [catName] => Budget conscious
                    [parentCategoryId] => ROC-CAT-10
                    [projectId] => 8
                    [catStatus] => 1
                    [visible] => 1
                    [modifiedDate] => 1443520431
                    [createDate] => 1443520431
                )

            [5] => Array
                (
                    [catId] => ROC-CAT-14
                    [catName] => Brands
                    [projectId] => 8
                    [catStatus] => 1
                    [visible] => 1
                    [modifiedDate] => 1443520488
                    [createDate] => 1443520488
                )

            [6] => Array
                (
                    [catId] => ROC-CAT-15
                    [catName] => Samsung
                    [parentCategoryId] => ROC-CAT-14
                    [projectId] => 8
                    [catStatus] => 1
                    [visible] => 1
                    [modifiedDate] => 1443520497
                    [createDate] => 1443520497
                )

            [7] => Array
                (
                    [catId] => ROC-CAT-16
                    [catName] => HTC
                    [parentCategoryId] => ROC-CAT-14
                    [projectId] => 8
                    [catStatus] => 1
                    [visible] => 1
                    [modifiedDate] => 1443520505
                    [createDate] => 1443520505
                )

            [8] => Array
                (
                    [catId] => ROC-CAT-17
                    [catName] => Sony Ericsson
                    [parentCategoryId] => ROC-CAT-14
                    [projectId] => 8
                    [catStatus] => 1
                    [visible] => 1
                    [modifiedDate] => 1443520605
                    [createDate] => 1443520605
                )

            [9] => Array
                (
                    [catId] => ROC-CAT-18
                    [catName] => Apple
                    [parentCategoryId] => ROC-CAT-14
                    [projectId] => 8
                    [catStatus] => 1
                    [visible] => 1
                    [modifiedDate] => 1443520617
                    [createDate] => 1443520617
                )

            [10] => Array
                (
                    [catId] => ROC-CAT-3
                    [catName] => iOS Phones
                    [parentCategoryId] => ROC-CAT-1
                    [projectId] => 8
                    [catMetaKeywords] => iOS Phones
                    [catMetaDesc] => iOS phones
                    [catStatus] => 1
                    [visible] => 1
                    [modifiedDate] => 1443467837
                    [createDate] => 1443467837
                )

            [11] => Array
                (
                    [catId] => ROC-CAT-4
                    [catName] => Android Phones
                    [parentCategoryId] => ROC-CAT-1
                    [projectId] => 8
                    [catMetaKeywords] => Android Phones
                    [catMetaDesc] => Android Phones
                    [catStatus] => 1
                    [visible] => 1
                    [modifiedDate] => 1443467907
                    [createDate] => 1443467907
                )

            [12] => Array
                (
                    [catId] => ROC-CAT-5
                    [catName] => Windows Phones
                    [parentCategoryId] => ROC-CAT-1
                    [projectId] => 8
                    [catStatus] => 1
                    [visible] => 1
                    [modifiedDate] => 1443519782
                    [createDate] => 1443468472
                )

            [13] => Array
                (
                    [catId] => ROC-CAT-6
                    [catName] => Laptops
                    [projectId] => 8
                    [catStatus] => 1
                    [visible] => 1
                    [modifiedDate] => 1443519807
                    [createDate] => 1443519807
                )

            [14] => Array
                (
                    [catId] => ROC-CAT-7
                    [catName] => Ultrabooks
                    [parentCategoryId] => ROC-CAT-6
                    [projectId] => 8
                    [catStatus] => 1
                    [visible] => 1
                    [modifiedDate] => 1443519829
                    [createDate] => 1443519829
                )

            [15] => Array
                (
                    [catId] => ROC-CAT-8
                    [catName] => Multimedia laptops
                    [parentCategoryId] => ROC-CAT-6
                    [projectId] => 8
                    [catStatus] => 1
                    [visible] => 1
                    [modifiedDate] => 1443519851
                    [createDate] => 1443519851
                )

            [16] => Array
                (
                    [catId] => ROC-CAT-9
                    [catName] => AIO laptops
                    [parentCategoryId] => ROC-CAT-6
                    [projectId] => 8
                    [catStatus] => 1
                    [visible] => 1
                    [modifiedDate] => 1443519915
                    [createDate] => 1443519915
                )

        )

    [totalCount] => 17
)

1 个答案:

答案 0 :(得分:0)

这对我有用

<ul class="menu" id="nav">
  <li>Shop By Categories</li>
  <?php 
    if ($_SESSION["loginSuccess"])
        $sessionId = $_SESSION["sessionId"];
    else
        $sessionId = $non_login_user;

    $fields = array (
            'projectId' => $PROJECT_ID,
            'sessionId' => $sessionId
    );
    $resultListAllCategories = get_decoded_info ( $SERVER_URL, $PORT, 'tos-product/user/category/listAll', $fields );

    foreach ( $resultListAllCategories as $key => $eachDetail ) {
        foreach ( $eachDetail as $key => $eachData ) {
            if(! isset($eachData['parentCategoryId'])){
                $x = $eachData['catId'];?>
    <li><a href="#"><?php echo $eachData['catName'];?></a>
      <div class="megadrop">
        <div class="col">
         <?php 
          $cur = 0;
            foreach ( $resultListAllCategories as $key => $eachDetail ) {
                foreach ( $eachDetail as $key => $eachData ) {
                    if(isset($eachData['parentCategoryId'])){
                        $y = $eachData['parentCategoryId'];

            if($y == $x) { 
                 if($cur == 0) {
                    echo '<ul>';
                }
                echo '<li>'.$eachData['catName'].'</li>';
            if($cur == 1) {
                echo '</ul>';
                $cur = 0;
            }
            else {
                $cur++;
            } }}
            }}
            ?>
        </div>
    </div>
    </li>
   <?php }}}?>
</ul>