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
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
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]]}));
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
var a = [ 'x' ,'y' , 'z'];
b = [ 'p', 'q', 'r']
var newArray = a.map(function(val,index){return [val,b[index]]});
console.log(newArray)
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);
});