-1

I have two arrays like this:

$array_a = array('a','b','c','d');
$array_b = array('e','f','g','h');

Now I need to display my array values in this format:

a is e
b is f
c is g
d is h

How can do it?

Rizier123
  • 58,877
  • 16
  • 101
  • 156
user3246727
  • 111
  • 1
  • 3
  • 13
  • 1
    Have you even tried something? Show us your attempts! – Rizier123 May 01 '15 at 19:08
  • 1
    @Rizier123 The asker isn't requesting large project help. This answer will be a snippit of code. Does he really need to show us an attempted snippit? – David Greydanus May 01 '15 at 19:11
  • 1
    @DavidGreydanus I think it would show that he don't just want us to the his job. It would also show that he's stuck and don't get any further and needs help. It also increases the quality of the post. – Rizier123 May 01 '15 at 19:12
  • @Rizier123 I needed help. The answer to that question. – user3246727 May 01 '15 at 19:20
  • @user3246727: Your question has been asked before by someone else (actually more than just one person). Please search for it first if you need an answer. – hakre May 03 '15 at 07:32

3 Answers3

3

This should work for you:

Just use array_map() to loop through both arrays:

array_map(function($v1, $v2){
    echo $v1 . " is " . $v2 . "<br>";
}, $array_a, $array_b);

output:

a is e
b is f
c is g
d is h

Big advantage? Yes, It doesn't matter if one array is longer than the other one!

Rizier123
  • 58,877
  • 16
  • 101
  • 156
2
 foreach($array_a as $key=>$value){
     echo $value.' is '.$array_b[$key];
 }

try like this

$key contains the current key in loop of the first array. Since you want the display the element from the second array at the same position, you just echo the element from second array with that key

0

Perhaps you might want to do something like this:

<?php
$a = array('a', 'b', 'c', 'd');
$b = array('e', 'f', 'g', 'h');
$c = array_combine($a, $b);

print_r($c);
?>

Output

Array
(
[a] => e
[b] => f
[c] => g
[d] => h
)