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