About Array in PHP
Like any other programming languages or web languages, array plays an important role in storing many data.
Storing single array elements in PHP in as any normal languages.
Syntax : array () ;
But to store many array within an array, there are Multidimensional Arrays. PHP have the Array of Array concept.
Syntax: array (array());
Array in Arrays in PHP to store database values.
Well this post seems to be simple. Yes , i had an issue with storing the values in multidimensional arrays, but there haven't been any proper post till yet, of actual way of storing and dynamic looping used for storing data.
So lets start,
I want to store watches database values. For each watch there are 5 parameters (coloumns).
First I will extract the data from database using select query
$res = mysql_query("select * from watches ") or die(mysql_error());
Next, we should initialize array of array for storing data
$wat = array(array());
Now, below is the simple snippet to dynamically store values in that $wat array.
$i=0;
while($row = mysql_fetch_array($res))
{
$j=0;
$wat[$i][$j++] = $row['brand'];
$wat[$i][$j++] = $row['modelname'];
$wat[$i][$j++] = $row['price'];
$wat[$i][$j++] = $row['gender'];
$wat[$i][$j++] = $row['movement'];
$i++;
}
Now if we want to display all the contents of data in that array, we can do something like this.
for($i=0 ; $i<$rows ; $i++)
{
for($j=0 ; $j< $columns; $j++)
{
echo $wat[$i][$j]."<br>";
}
}
So we had store and displayed the data from the database successfully.
If you liked the post, do share and like us at AnonHackSociety