-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?

5
  • 1
    Have you even tried something? Show us your attempts! Commented May 1, 2015 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? Commented May 1, 2015 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. Commented May 1, 2015 at 19:12
  • @Rizier123 I needed help. The answer to that question. Commented May 1, 2015 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. Commented May 3, 2015 at 7:32

3 Answers 3

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!

Sign up to request clarification or add additional context in comments.

3 Comments

@user3246727 You're welcome! (You can still add your attempts to your question, which increases the quality of your post!)
@user3246727 So is this question answered? As long as you didn't accepted any answer it still remains as unanswered on the Stack
no callback needed, use NULL instead ;)
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

Comments

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
)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.