-2

I have two arrays like this:

$path = array("Login", "Register");
$list = array("Admin", "Customers", "Guest");

for ($i=0, $i<=count($path), $i++, $k=0; $k<=count($list); $k++) {
    echo "Var " . $path[$i] . " is " . $list[$k] . "\n";
}

Output :

Var Register is Admin
Var Register is Customers
Var Register is Guest
<br />
<b>Notice</b>:  Undefined offset: 3 in <b>[...][...]</b> on line <b>6</b><br />
Var Register is 

I need Output this

Var Login is Admin
Var Login is Customers
Var Login is Guest
Var Register is Admin
Var Register is Customers
Var Register is Guest

Any have solution?

  • @RolandStarke can write the code ? – alifia avirista Nov 27 '18 at 10:19
  • @alifiaavirista - Have you thought about using a [dictionary](https://stackoverflow.com/questions/6490482/are-there-dictionaries-in-php)? – tomerpacific Nov 27 '18 at 10:23
  • Welcome to stackoverflow.com. Please take some time to read the [help pages](http://stackoverflow.com/help), especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). Also please [take the tour](http://stackoverflow.com/tour) and read about [how to ask good questions](https://stackoverflow.com/help/how-to-ask). Lastly please read [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Spangen Nov 27 '18 at 11:33

4 Answers4

0

You need to use a second foreach.

<?php
$path = array("Login", "Register");
$list = array("Admin", "Customers", "Guest");

foreach ($path as $p) {
    foreach ($list as $l) { 
        echo "Var " . $p . " is " . $l . "\n";
    }
}
executable
  • 3,365
  • 6
  • 24
  • 52
0

If you just want to walk over array you can use foreach loop:

foreach ($path as $p) {
    $row = 'Var ' . $p . ' is ';
    foreach ($list as $l) {
        echo $row . $l . "\n";
    }
}

where first loop walks through vars and then inner loop uses every var for every list item

Artem Ilchenko
  • 1,050
  • 9
  • 20
0
$path = array("Login", "Register");
$list = array("Admin", "Customers", "Guest");

for ($i=0; $i<count($path); $i++) {
    for($j=0;$j<count($list);$j++){
       echo "Var " . $path[$i] . " is " . $list[$k] . "\n";
    }
}

Hope this will help you

MD. Jubair Mizan
  • 1,539
  • 1
  • 12
  • 20
0
<?php
$path = array("Login", "Register");
$list = array("Admin", "Customers", "Guest");

foreach ($path as $row) {
    foreach ($list as $data) {
        echo "Var " . $row . " is " . $data . "\n";
        echo '<br />';
    }
}

You just need a double foreach loop to parse the arrays.

The output is :

Var Login is Admin 
Var Login is Customers 
Var Login is Guest 
Var Register is Admin 
Var Register is Customers 
Var Register is Guest 
pr1nc3
  • 8,108
  • 3
  • 23
  • 36