0

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

cryptohustla
  • 45
  • 1
  • 7

3 Answers3

2

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.
    }
}
Sahil Gulati
  • 15,028
  • 4
  • 24
  • 42
  • i am using your solution just 1 quick question am i really oblige to use ini_set('display_errors', 1);? thanks – cryptohustla Apr 17 '17 at 04:53
  • @cryptohustla You should use this `ini_set('display_errors', 1);` so that you can check whether is there any error warnings ,notices in your code. Which will further help you in identifying further issues. – Sahil Gulati Apr 17 '17 at 04:55
  • but is it fine to turn it off if there is no error? – cryptohustla Apr 17 '17 at 05:10
  • turn it off like instead of 1 ill just put 0 – cryptohustla Apr 17 '17 at 05:11
  • @cryptohustla Yes its fine to turn it off if you want my recommendation is to turn it on, yes you can put `0` instead of `1` to turn it off. :) – Sahil Gulati Apr 17 '17 at 05:12
  • i dont see errors anyway but i keep seeing warnings ... so i just turned it off anyway i have another question should i just ask here or open up another question? your solution solves my problem however how can i prevent the loop from showing empty arrays.. those 5 variables $branchNameList, $branchPhotoList and so on have the same number 1-50 and if $branchNameList is empty obviously other variables are also empty.. so how can i prevent the loop from showing empty? thanks a lot – cryptohustla Apr 17 '17 at 05:21
  • well it work what i did is just use 1 conditional statement since i assume that if 1 of the variable is empty then it should not show all .. :) – cryptohustla Apr 17 '17 at 05:49
  • Yeah thats perfectly fine, You can modify the code the way you want, its totally upto you ... :) – Sahil Gulati Apr 17 '17 at 05:50
  • 1 last question sahil :D is there any shortcut to make the array short? – cryptohustla Apr 17 '17 at 05:58
  • like currently this is the arrays... 1-50 all are connected to each other and its too long im hoping i can me it short since the only difference is the numbers .. – cryptohustla Apr 17 '17 at 06:00
  • @cryptohustla You can cut short your code in this way, I hope it will fine.. :) There can also be no. of ways – Sahil Gulati Apr 17 '17 at 06:17
  • no i mean the arrays :D like the code below which is too long actually all the variables have the same incremental value of up to 50 $branchNameList = array("branch_name1","branch_name2","branch_name3","branch_name4","branch_name5","branch_name6","branch_name7","branch_name8","branch_name9","branch_name10","branch_name11","branch_name12","branch_name13","branch_name14","branch_name15","branch_name16","branch_name17","branch_name18","branch_name19","branch_name20","branch_name21","branch_name22","branch_name23","branch_name24","branch_name25"); – cryptohustla Apr 17 '17 at 08:35
  • @cryptohustla Noop i dont think minimizing this array could be a good approach. – Sahil Gulati Apr 17 '17 at 08:43
  • @cryptohustla I was expecting my answer is acceptable for you. – Sahil Gulati Apr 17 '17 at 10:23
  • yes i accepted the answer, thank you so much for your time and effort really appreciate it – cryptohustla Apr 17 '17 at 10:40
  • @cryptohustla Welcome sir... :) we are here to help you out. no issues – Sahil Gulati Apr 17 '17 at 10:45
  • cryptohustla, assuming you really want the shown content, then yes, there are shorter ways, for example: `$branches = []; for ($i=1; $i<26; ++$i) { $branches[$i] = "branch_name" . $i; }` - check http://ideone.com/tXoJ0B – domsson Apr 17 '17 at 11:05
1

if you can be sure that your arrays always have the same count and that they are all related :

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

?>
OldPadawan
  • 1,247
  • 3
  • 16
  • 25
  • Nice. So obvious but I have never thought of it! Always resort to a for loop in such scenarios. – inarilo Apr 16 '17 at 17:52
1

In that case, you should use a regular for loop instead of 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>";
}

However, you will run into issues if some of the arrays have less elements than the one you use as condition for the loop (here: $branchNameList). There are at least two ways you could tackle that problem:

  1. Figure out the smallest of these arrays and use its length as condition.
  2. Use the length of the largest array as condition and add a safeguard within the loop.

Let's see an example for the first option:

/* ... */

$minLength = min(
    count($branchNameList),
    count($branchPhotoList),
    count($branchAddList)
);

for ($i = 0; $i < $minLength; ++$i) {
    /* ... */
}

And one for the second option:

/* ... */

$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>";
    }
}
domsson
  • 4,553
  • 2
  • 22
  • 40