I have previously written the code below with a loop to echo the keys of a two dimensional array.
$coordinates = array(
"x"=>array(1,2,3),
"y"=>array(4,5,6)
);
foreach($coordinates as $xycoordinates => $position){
echo "Position: ". $xycoordinates."<br/>";
foreach($position as $key => $value){
echo $value;
}
echo "<br /><br />";
}
I'm trying to populate the array with some random numbers. Each attempt keeps referencing 'array push', which isn't a loop (or can array push be used in a loop?).
Explanation: in code below,
$Anynumber = 10;
this statement is simply creating a variable i.e. declaring and allocating memory space and also initializing it with value '10' ,
$Array = array();
In this statement, an array is being declared but NOT initialized with any value, in php Array can increase and decrease their size as per requirement,
for($index=0 ; $index<$Anynumber; $index++)
this is simple 'for' loop, from 0 to 9 as condition is " less than $Anynumber which is 10,
$Array[] = $index;
This statement is simply assigning $index's value which is increased at each iteration of 'for' loop,
e.g.
$Array[] = $index; // at first iteraion its like this $Array[0] = 0;
$Array[] = $index; // at second iteraion its like this $Array[1] = 1;
$Array[] = $index; // at third iteraion its like this $Array[2] = 2;
and so on.
I hope this cleared any confusion you had.
complete code:
$Anynumber = 10;
$Array = array();
for($index=0 ; $index<$Anynumber ; $index++){
$Array[] = $index;
}
I would do something like:
$size = 3;
$coordinates = array(
"x"=>array(),
"y"=>array()
);
for ($i = 0; $i < $size ; $i++) {
array_push($coordinates["x"], rand(1,10));
array_push($coordinates["y"], rand(1,10));
}