单个foreach中的多个数组

时间:2017-04-16 17:06:06

标签: php foreach

我今天有一个疯狂的夜晚,因为我无法解决它,我无法找到任何解决方案,我能找到的是一个解决方案,如果它只有2个阵列,但我有超过2.我的问题是可以循环多个阵列单一的foreach?下面的代码适用于branchname但不适用于其他数组

<?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>';
                }
?>

请有人帮帮我吗?

谢谢

3 个答案:

答案 0 :(得分:2)

你期待这个吗?确保在所有其他数组的count相同时运行循环,否则您将获得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;
    }
}

解决方案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.
    }
}

答案 1 :(得分:1)

如果您可以确定您的数组总是具有相同的计数并且它们都是相关的:

<?php

error_reporting(E_ALL);
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");

foreach($branchNameList as $key => $value){ /* we access 1st level */
echo '** key: '.$key.' / value: '.$value.' **<br/>';
echo $branchPhotoList[$key].'<br/>'; /* then we use 1st level key */
echo $branchAddList[$key].'<br/>';
echo $branchMapList[$key].'<br/>';
echo $branchTelList[$key].'<br/>';
}

?>

答案 2 :(得分:1)

在这种情况下,您应该使用常规for循环而不是foreach

$branchNameList = array("branch_name1", "branch_name2", "branch_name3", "branch_name4");
$branchPhotoList = array("photo1", "photo2", "photo3", "photo4");
$branchAddList = array("address1", "address2", "address3", "address4");

for ($i = 0; $i < count($branchNameList); ++$i) {
    echo $branchNameList[$i] . "<br>";
    echo $branchPhotoList[$i] . "<br>";
    echo $branchAddList[$i] . "<br>";
}

但是,如果某些数组的元素少于用作循环条件的元素(例如:$branchNameList),则会遇到问题。至少有两种方法可以解决这个问题:

  1. 找出这些数组中最小的数组并将其长度用作条件。
  2. 使用最大数组的长度作为条件,并在循环中添加安全措施。
  3. 让我们看一下第一个选项的例子:

    /* ... */
    
    $minLength = min(
        count($branchNameList),
        count($branchPhotoList),
        count($branchAddList)
    );
    
    for ($i = 0; $i < $minLength; ++$i) {
        /* ... */
    }
    

    第二个选项之一:

    /* ... */
    
    $maxLength = max(
        count($branchNameList),
        count($branchPhotoList),
        count($branchAddList)
    );
    
    for ($i = 0; $i < $maxLength; ++$i) {
        if (isset($branchPhotoList[$i]) {
            echo $branchNameList[$i] . "<br>";
        }
        if (isset($branchPhotoList[$i]) {
            echo $branchPhotoList[$i] . "<br>";
        }
        if (isset($branchPhotoList[$i]) {
            echo $branchAddList[$i] . "<br>";
        }
    }