Hi Developer,
This article will learn about multi dimensional arrays in PHP. multi dimensional is array of array
Introduction
we will introduce you to two-dimensional arrays in PHP. We have discussed the arrays before containing one key against each value, but multidimensional arrays consist of arrays of arrays. So, there can be multiple keys against a value in multidimensional arrays.
What is two-dimensional arrays in Php
A two-dimensional array is an array of arrays. For your better understanding, we would like to define the three-dimensional array as an array of arrays of arrays.
So, dimensionality refers to the number of arrays within arrays.
Syntax of the two-dimensional array in Php
The general syntax of two-dimensional array in Php is:
array (
array (elements...),
array (elements...),
...
)
Example of the two-dimensional array in Php
Let’s make show you practical example of a two-dimensional array in Php. For this purpose, consider the following data table. We want to store this table’s data into a two-dimensional array.
Name Stock Sold
AA 21 12
BB 12 9
CC 22 1
DD 17 12
The above table contains two parameters for a complete description of each row of stock and sold data. A one-dimensional array cannot store this information because we need more than one key to point to this data. Solving this problem by using the two-dimensional array approach.
$demo = array (
array("AA",21,12),
array("BB",12,9),
array("CC",22,1),
array("DD",17,12)
);
The main array will hold four arrays forming the two dimensions inside the memory. These four arrays consist of two indices to store Stock and Sold values. For getting a clear idea of how a 2D array forms its dimensions, refer to the figure above in this article.
How to access values in two-dimensional array Php
<?php
echo $demo[0][0].": In stock: ".$demo[0][1].", sold: ".$demo[0][2].".<br>";
echo $ demo[1][0].": In stock: ".$demo[1][1].", sold: ".$demo[1][2].".<br>";
echo $ demo[2][0].": In stock: ".$demo.", sold: ".$demo[2][2].".<br>";
echo $ demo[3][0].": In stock: ".$demo.", sold: ".$demo[3][2].".<br>";
?>
The next question that is arising in your mind will surely be how to traverse a two-dimensional array. In this next section, we will answer it.
How to traverse two-dimensional arrays in Php
Like we have used a loop to traverse the one-dimensional array, we use the same approach to traverse the two-dimensional array as well, but with little different logic.
<?php
for ($r = 0; $r < 4; $r++) {
echo "<p><b>Row number $r</b></p>";
echo "<ul>";
for ($c = 0; $c < 3; $c++) {
echo "<li>".$demo[$r][$c]."</li>";
}
echo "</ul>";
}
?>
We have used nested for loop i:e for loop inside for loop. The inner for loop is responsible for printing columns, and it executes 3 times for every single run of the outer loop, which is responsible for printing the rows of the vehicle array.
* Note: You can use the count function instead of specifying the number of rows and columns.
* Note: var_dump()
function in Php displays the structure of an array.
Try It
<?php
echo "Welcome to multi dimensional arrays in php <br>";
// Creating a two-dimensional array
$multiDim = array(array(2,5,7,8),
array(1,2,3,1),
array(4,5,0,1));
// echo var_dump($multiDim);
// echo $multiDim[1][2];
// Printing the contents of a two-dimensional array
// for ($i=0; $i < count($multiDim); $i++) {
// echo var_dump($multiDim[$i]);
// echo "<br>";
// }
for ($i=0; $i < count($multiDim); $i++) {
for ($j=0; $j < count($multiDim[$i]); $j++) {
echo $multiDim[$i][$j];
echo " ";
}
echo "<br>";
}
?>
Hope it can help you…
Categories : PHP
buy tricor 200mg without prescription generic tricor 160mg purchase tricor pill
buy generic zaditor for sale buy doxepin online cheap buy imipramine generic
cialis 40mg canada viagra for men sildenafil citrate
minoxidil canada flomax 0.2mg us best erection pills
buy aspirin without a prescription buy aspirin 75 mg for sale order imiquad for sale
buy dipyridamole cheap buy felodipine 10mg without prescription pravastatin for sale online
melatonin 3mg pill order desogestrel for sale danocrine 100mg cost
purchase duphaston online cheap order januvia 100mg empagliflozin 25mg sale
prasugrel cost order dramamine 50mg pills buy tolterodine 1mg for sale
monograph online order buy colospa pills generic pletal 100mg
order ferrous for sale buy ascorbic acid 500 mg pills purchase betapace for sale
buy pyridostigmine online buy maxalt 5mg rizatriptan brand
zovirax online buy buy exelon 6mg without prescription buy generic rivastigmine 6mg
buy prilosec 10mg generic buy generic prilosec 10mg purchase metoprolol pills
premarin buy online cheap viagra viagra 100mg england
telmisartan 80mg price micardis canada buy generic molnunat over the counter
cenforce 50mg uk oral cenforce 100mg order aralen 250mg generic
cialis 5mg uk order tadalafil 20mg generic sildenafil 100mg cost
buy cefdinir without prescription buy omnicef pills buy lansoprazole 15mg without prescription
oral provigil buy promethazine 25mg online cheap buy deltasone 40mg generic
buy generic lipitor 40mg order proventil 100 mcg generic oral norvasc 10mg
buy accutane pills order amoxicillin 250mg for sale generic zithromax 500mg
azithromycin 500mg tablet buy cheap generic neurontin buy gabapentin generic
buy protonix online protonix for sale phenazopyridine 200mg ca
poker online for money furosemide order online furosemide without prescription
free games poker online ventolin order albuterol oral
order symmetrel 100mg pill buy generic dapsone over the counter order avlosulfon 100mg online
poker online free online casino with free signup bonus real money usa ivermectin 3
methylprednisolone 8 mg online buy cheap generic methylprednisolone cost aristocort 10mg
best online casino games augmentin 1000mg pills buy synthroid 100mcg generic
how to buy clomid isosorbide 20mg ca buy imuran 25mg without prescription
coversyl tablet coversum drug brand fexofenadine
order vardenafil 10mg sale tizanidine 2mg without prescription buy generic zanaflex online
phenytoin order online order generic oxybutynin 5mg ditropan brand
loratadine 10mg price loratadine 10mg ca buy dapoxetine tablets
cost ozobax elavil where to buy toradol oral
buy generic amaryl 4mg misoprostol 200mcg without prescription order arcoxia
buy ozobax pills buy generic lioresal over the counter toradol for sale online
alendronate 35mg cheap macrodantin over the counter buy furadantin 100 mg generic
how to buy inderal inderal generic plavix 75mg usa
buy generic pamelor over the counter where to buy acetaminophen without a prescription purchase panadol pill
purchase orlistat sale buy mesalamine online cheap diltiazem 180mg uk
coumadin buy online reglan 10mg over the counter brand metoclopramide 10mg
order generic azelastine 10ml acyclovir generic buy avapro 300mg generic
pepcid generic buy hyzaar for sale order tacrolimus 1mg for sale
nexium 40mg drug order topamax 100mg online cheap topiramate 200mg cheap
purchase allopurinol pill rosuvastatin 20mg ca oral crestor 10mg
imitrex tablet buy levofloxacin pill dutasteride sale
buspirone for sale buspin cheap buy cordarone pills
ranitidine medication buy celebrex 200mg celebrex ca
motilium 10mg generic buy domperidone 10mg online cheap cost tetracycline 500mg
tamsulosin 0.4mg over the counter purchase zocor pill buy generic simvastatin online
purchase essay online money can t buy everything essay essays done for you
buy generic aldactone for sale buy aldactone tablets finpecia sale
buy sildenafil 100mg without prescription order estrace without prescription estradiol online
buy diflucan 200mg without prescription forcan generic buy cipro 500mg online cheap
lamotrigine 200mg over the counter order generic minipress purchase nemazole pill
buy metronidazole 200mg online cheap buy cephalexin paypal order cephalexin 125mg
buy tretinoin generic avana 200mg us cost avana
buy clindamycin for sale buy ed medication where to buy fildena without a prescription
buy tadacip 20mg generic order indocin 50mg pill indocin without prescription
order nolvadex 10mg for sale buy symbicort tablets budesonide spray
cost lamisil top rated free slots online online casino slots
order ceftin 250mg online cheap methocarbamol cheap buy methocarbamol 500mg generic
order trazodone 100mg sale order trazodone 100mg generic clindamycin drug
help with assignments uk phd thesis proposal generic suprax 100mg
aspirin 75mg price slot games online casino slot
buy generic trimox for sale order clarithromycin for sale biaxin over the counter
how to write an essay about my family academic writers online poker sites
order catapres 0.1mg online buy antivert 25 mg pills order spiriva 9 mcg generic
oral calcitriol 0.25 mg calcitriol 0.25mg usa order tricor 200mg generic
brand minocin 50mg minocycline 100mg sale cheap requip
buy pills to treat acne buy oxcarbazepine 600mg pills buy oxcarbazepine 300mg generic
alfuzosin over the counter uroxatral 10 mg ca heartburn covered by medicaid
femara 2.5mg ca albenza 400mg brand buy aripiprazole generic
buy sleep medications online online prescriptions weight loss medication amazingweightloss best weight loss pills
buy provera 5mg sale biltricide 600mg for sale buy microzide cheap
new medications for smoking cessation strongest otc pain reliever strong painkillers online sale
periactin buy online cost fluvoxamine 100mg buy nizoral 200 mg generic
best online herpes medication nhs leaflet type 2 diabetes feline diabetes insulin chart
cymbalta medication order provigil 100mg online order modafinil for sale
anti fungi tablets without perscriptions antianxiety medication for genital herpes instant high blood pressure relief
cost phenergan promethazine 25mg tablet ivermectin 4000
duodenal erosion treatment how to get rid of a urine infection fast types of urinary bacterial infections
buy prednisone 10mg generic amoxicillin 500mg for sale generic amoxil 1000mg
best cheap birth control pill does ibuprofen help with prostatitis prigile dapoxetine 30 mg
zithromax canada order neurontin 600mg pill order neurontin 100mg sale
can you take too much vitamin c anti nausea prescription medication list best pills for flatulence
order ursodiol 150mg pill actigall 300mg brand buy cetirizine pills
buy furosemide online buy furosemide 40mg without prescription albuterol online
strattera 10mg pills buy seroquel 50mg generic order generic zoloft
lexapro uk order revia online order revia 50 mg without prescription
buy generic augmentin 1000mg order clomiphene generic order clomid 100mg online
buy combivent 100 mcg pill generic ipratropium zyvox drug
buy levitra pills for sale vardenafil order online order generic hydroxychloroquine
starlix 120mg brand candesartan 8mg canada candesartan 16mg over the counter
cenforce 100mg over the counter buy chloroquine 250mg without prescription purchase glycomet online
buy cheap generic tegretol lincocin medication lincomycin 500mg for sale
how to buy lipitor amlodipine 10mg cheap zestril 5mg pill
duricef 500mg usa ascorbic acid pills lamivudine pills
brand prilosec 10mg prilosec cheap atenolol 50mg us
cabergoline brand claritin order buy generic dapoxetine
purchase depo-medrol generic clarinex where to buy buy desloratadine paypal
order cytotec 200mcg pills orlistat 120mg without prescription brand diltiazem
buy generic nootropil piracetam 800mg pills cost anafranil 50mg
acyclovir us purchase allopurinol for sale rosuvastatin pill
buy sporanox pills for sale tindamax without prescription tindamax pills
order ezetimibe 10mg pill buy cheap sumycin buy sumycin 250mg
cheap olanzapine 10mg buy diovan 160mg valsartan 80mg us
purchase flexeril pills purchase toradol pills toradol oral
colchicine pills cost inderal 10mg order methotrexate 5mg generic
acne medication names pills order betamethasone antibiotics for pimple pills
best generic allegra buy generic astelin prescription vs over the counter
top rated heartburn relief combivir over the counter
best online rx for sleep cost promethazine
buy deltasone pill order prednisone pills
best medication for gerd symptoms cost epivir
best oral treatment for acne betnovate 20 gm cream low cost prescription acne medication
piriton allergy tablets canada order generic advair diskus inhalator doctor prescribed allergy medication
treat heartburn without antacids purchase cefadroxil
buy accutane cheap isotretinoin 20mg us accutane generic
sleep aids prescription drugs order meloset 3mg online cheap
buy amoxicillin 250mg online cheap buy amoxicillin 500mg generic order generic amoxicillin 250mg
buy zithromax without prescription purchase azithromycin sale zithromax 500mg oral
buy gabapentin generic gabapentin 600mg without prescription
buy azipro 500mg sale azipro online order azithromycin
purchase lasix generic order furosemide without prescription
buy prednisolone pills for sale buy omnacortil 20mg pill omnacortil 5mg oral
prednisone 5mg oral buy prednisone 5mg sale
buy amoxicillin 250mg for sale purchase amoxil generic oral amoxicillin 1000mg
buy monodox pill buy acticlate
albuterol 4mg cost purchase albuterol online buy ventolin 4mg online
amoxiclav brand augmentin 1000mg us
generic synthroid 75mcg buy levoxyl generic levothroid without prescription
levitra online order buy generic levitra
order clomid clomid brand generic clomiphene 100mg
tizanidine drug zanaflex sale purchase tizanidine
buy semaglutide 14mg without prescription order semaglutide 14mg generic cheap semaglutide 14 mg
order prednisone generic prednisone 20mg drug brand deltasone
semaglutide tablet cost semaglutide 14 mg rybelsus 14 mg brand
accutane tablet isotretinoin 10mg generic order isotretinoin 40mg for sale
buy albuterol for sale ventolin generic purchase albuterol pill
augmentin 625mg sale augmentin 1000mg brand augmentin 625mg cheap
zithromax us zithromax 500mg brand purchase zithromax online
buy synthroid without a prescription cheap generic levothyroxine buy synthroid 100mcg pill
prednisolone online buy buy omnacortil 5mg online cheap buy prednisolone 5mg generic
slot games online slot casino casino games online real money
purchase vardenafil online cheap purchase levitra generic order levitra 10mg for sale
lyrica online order lyrica lyrica 75mg oral
hydroxychloroquine 400mg cost order plaquenil 200mg generic plaquenil 200mg for sale
buy triamcinolone 10mg online cheap buy aristocort generic buy aristocort no prescription
tadalafil for sale online order tadalafil 5mg without prescription buy tadalafil online cheap
desloratadine sale oral clarinex generic clarinex
cenforce medication buy cenforce generic cenforce over the counter
claritin 10mg us claritin online buy buy claritin generic
chloroquine cheap order aralen 250mg pill buy aralen pill
buy xenical 60mg pill buy orlistat 120mg online cheap diltiazem over the counter
buy atorvastatin 20mg sale buy atorvastatin 80mg buy lipitor without prescription
buy acyclovir 800mg without prescription order zovirax 800mg online zyloprim online order
norvasc 10mg canada cost norvasc 5mg amlodipine 5mg cost
lisinopril canada order zestril 10mg online cheap order prinivil pills
order rosuvastatin 10mg without prescription zetia order zetia 10mg canada
buy omeprazole online cheap prilosec usa omeprazole price
motilium generic order motilium 10mg without prescription order sumycin pill
buy lopressor 50mg pills buy lopressor 100mg pill buy lopressor 100mg online cheap
purchase cyclobenzaprine generic lioresal order buy generic baclofen online
buy atenolol 50mg online atenolol ca order atenolol 50mg online cheap
buy toradol sale toradol generic cheap gloperba
buy generic medrol buy generic depo-medrol buy methylprednisolone paypal