douzhi3776 2017-04-16 17:06
浏览 38
已采纳

单个foreach中的多个数组

I have a crazy night today as i cant solve it and i cant find any solution all i can find is a solution if its only 2 arrays but i have more than 2. My questions is it possible to loop multiple arrays in a single foreach? the code below works for branchname but not on the other array

<?php

$branchNameList = array("branch_name1", "branch_name2", "branch_name3", "branch_name4");
$branchPhotoList = array("photo1", "photo2", "photo3", "photo4");
$branchAddList = array("address1", "address2", "address3", "address4");
$branchMapList = array("maplink1", "maplink2", "maplink3", "maplink4");
$branchTelList = array("branch_tel1", "branch_tel2", "branch_tel3", "branch_tel4");

foreach ($branchNameList as $branchName) {
    echo '<div>
                '.$params->get($branchPhoto).'
            </div>
                    <h2>
                        '.$params->get($branchName).'
                    </h2>
                    <div>
                        <a href="'.$params->get($branchMap).'">'.$params->get($branchAdd).'</a>
                    </div>
                    <div>
                        <a href="#">'.$params->get($branchTel).'</a>
                </div>';
                }
?>

please can someone help me?

thank you

  • 写回答

3条回答 默认 最新

  • doucan957495 2017-04-16 17:10
    关注

    Are you expecting this? Make sure to run for loop when count of all other arrays are same, Else you will get the Notice error

    Try this code snippet here

    <?php
    ini_set('display_errors', 1);
    $branchNameList = array("branch_name1", "branch_name2", "branch_name3", "branch_name4");
    $branchPhotoList = array("photo1", "photo2", "photo3", "photo4");
    $branchAddList = array("address1", "address2", "address3", "address4");
    $branchMapList = array("maplink1", "maplink2", "maplink3", "maplink4");
    $branchTelList = array("branch_tel1", "branch_tel2", "branch_tel3", "branch_tel4");
    for($x=0;$x<count($branchNameList);$x++)
    {
        if(!empty($branchNameList[$x]))
        {
            echo $branchNameList[$x];
            echo PHP_EOL;
        }
        if(!empty($branchPhotoList[$x]))
        {
            echo $branchPhotoList[$x];
            echo PHP_EOL;
        }
        if(!empty($branchAddList[$x]))
        {
            echo $branchAddList[$x];
            echo PHP_EOL;
        }
        if(!empty($branchMapList[$x]))
        {
            echo $branchMapList[$x];
            echo PHP_EOL;
        }
        if(!empty($branchTelList[$x]))
        {
            echo $branchTelList[$x];
            echo PHP_EOL;
        }
    }
    

    Solution 2:

    <?php
    ini_set('display_errors', 1);
    $branchNameList = array("branch_name1", "branch_name2", "branch_name3", "branch_name4");
    $branchPhotoList = array("photo1", "photo2", "photo3", "photo4");
    $branchAddList = array("address1", "address2", "address3", "address4");
    $branchMapList = array("maplink1", "maplink2", "maplink3", "maplink4");
    $branchTelList = array("branch_tel1", "branch_tel2", "branch_tel3", "branch_tel4");
    $completeData=  array_merge(
            array("branchNameList"=>$branchNameList),
            array("branchPhotoList"=>$branchPhotoList),
            array("branchAddList"=>$branchAddList),
            array("branchMapList"=>$branchMapList),
            array("branchTelList"=>$branchTelList)
            );
    
    for($x=0;$x<count($completeData["branchNameList"]);$x++)
    {
        if( empty($completeData["branchNameList"][$x])||
            empty($completeData["branchPhotoList"][$x])||
            empty($completeData["branchAddList"][$x])||
            empty($completeData["branchMapList"][$x])||
            empty($completeData["branchTelList"][$x]))
        {
            continue;
        }
        else
        {
            //do what you want to do.
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助