-1

Print respective index value from two arrays in " Javascript " and in "PHP"

var  a = [ x , y , z] 
var b = [ p, q, r] 

then => values [x, p] , [ y, q]

please help me, Thanks you

4 Answers4

0

You can use Array.prototypr.map. See below code

In php it should be like this.

$a=array("x","y","z");
$b=array("p","q","r");
$c=array_map(null,$a,$b);
print_r($c);

x=[ "x" , "y" , "z"];
y=[ "p" , "q", "r"];

console.log(x.map((x1,i)=>{return [x1,y[i]]}));
yajiv
  • 2,901
  • 2
  • 15
  • 25
0

or just straight forward:

x=[ "x" , "y" , "z"];
y=[ "p" , "q" , "r"];

for (var i = 0; i < x.length; ++i) {
  alert('value at index [' + i + '] is: [' + x[i] + '] and [' + y[i] + ']');
}

as per comment in PHP:

$x= array("x" , "y" , "z");
$y= array("p" , "q" , "r");

for ($i = 0; $i < count($x); ++$i) {
    echo $x[$i];
    echo $y[$i];
}

also check this excellent answer here

oetoni
  • 3,269
  • 5
  • 20
  • 35
0

var a = [ 'x' ,'y' , 'z'];
b = [ 'p', 'q', 'r']
 var newArray = a.map(function(val,index){return [val,b[index]]});
 console.log(newArray)
John Willson
  • 444
  • 1
  • 3
  • 13
0

const daysNums = [1, 2, 3, 4, 5, 6, 7];
const daysNames = [
  "monday",
  "tuesday",
  "wednesday",
  "thursday",
  "friday",
  "saturday",
  "sunday"
];

daysNums.map((num, index) => {
  const f = [num, daysNames[index]];
  console.log(f);
});
Lee Taylor
  • 7,761
  • 16
  • 33
  • 49