2017-04-16 57 views
0

今天我有一個瘋狂的夜晚,因爲我無法解決它,我無法找到任何解決方案,所有我能找到的是一個解決方案,如果它只有2個陣列,但我有超過2個。我的問題是有可能的在一個foreach中循環多個數組?下面的代碼適用於branchname而不適用於其他陣列單個foreach中的多個數組

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

請問有人可以幫我嗎?

謝謝

+1

只是用於循環 – splash58

+0

'我有一個瘋狂的夜晚today' - >因爲你有所有你需要的答案,所以我可以說'現在晚安,享受你的睡眠':) – OldPadawan

+0

是啊謝謝:D @oldpalawan – cryptohustla

回答

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. 
    } 
} 
+0

我使用你的解決方案只是1個快速問題,我真的很想使用ini_set('display_errors',1);?謝謝 – cryptohustla

+0

@cryptohustla你應該使用這個'ini_set('display_errors',1);'這樣你就可以檢查代碼中是否有錯誤警告和聲明。這將進一步幫助您確定更多問題。 –

+0

,但如果沒有錯誤,可以關閉它嗎? – cryptohustla

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

?> 
+0

不錯。如此明顯,但我從未想過它!在這種情況下總是訴諸於for循環。 – inarilo

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. 使用最大數組的長度作爲條件並在循環中添加安全措施。

讓我們來看看第一個選項的例子:

/* ... */ 

$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>"; 
    } 
} 
+0

感謝您的簡要解釋並花時間回答我的問題:D – cryptohustla