0

I have two arrays:

$listx= array('a','b')

$listy= array('one','two','three','four','five','six','seven')

I would like to display array data in the format given below:

a one,

b two,

a three,

b four,

a five,

b six,

a seven

How can I do this?

Thanks in advance for the help.

devpro
  • 16,184
  • 3
  • 27
  • 38

2 Answers2

3

You can use this example:

$listx= array('a','b');

$listy= array('one','two','three','four','five','six','seven');

$i= 0;
foreach($listy as $value){

if($i == 2){
$i = 0; // reset as 0
}
echo $listx[$i] . " ". $value ."<br>";

$i++;
}

Explanation:

In this example most important thing is that you must know what is this:

if($i == 2)
{ 
   $i = 0; // reset as 0 
}

When $i is equal to 2 than you must need to reset as 0 otherwise you will get the undefined index or offset notices.

devpro
  • 16,184
  • 3
  • 27
  • 38
0

As your question ,a position is the divisible by 2 in keys value of $listy array ..

 foreach ($listy as $key => $value) {
    ($key % 2 == 0) ? var_dump($listx[0].$value):var_dump($listx[1].$value);        
}
Jack jdeoel
  • 4,554
  • 5
  • 26
  • 52