Multi-Dimensional Array Help

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
User avatar
Xaos
Posts: 940
Joined: Wed Jan 11, 2012 4:01 am

Multi-Dimensional Array Help

Post by Xaos »

Code: Select all

$gap = array(
array(1,'status'),
array(2,'status'),
array(3,'status'),
array(4,'status'),
array(5,'status'),
array(6,'status'),
array(7,'status'),
array(8,'status'),
array(9,'status'),
array(10,'status'),
);
 
So I have this array. I want to check all of the numerical values to another value, and I want to check the second value to a
String. I know I can use a for loop for a normal array, but not sure how for a multidimensional. Thanks for help :D
User avatar
hallsofvallhalla
Site Admin
Posts: 12023
Joined: Wed Apr 22, 2009 11:29 pm

Re: Multi-Dimensional Array Help

Post by hallsofvallhalla »

in the current example I am not sure why your first value needs to be numbers, just use indexes unless of course your index will not match the number. $gap[1] = 'status'

otherwise

Code: Select all

for(var i =0; i<gap.length;i++)
{
for(var l=0;l<gap[i].length;l++)
{
console.log(gap[i][l];
}

}
User avatar
Xaos
Posts: 940
Joined: Wed Jan 11, 2012 4:01 am

Re: Multi-Dimensional Array Help

Post by Xaos »

Oh man, I hadn't even thought of using just the index as the gap. Thanks for that, and the solution to the multi-problem.

Oh,another relating problem. For some reason, I tried to use $gap.length for the loop but it gave me an error, so I had to do count($gap); what was I doing wrong?
User avatar
hallsofvallhalla
Site Admin
Posts: 12023
Joined: Wed Apr 22, 2009 11:29 pm

Re: Multi-Dimensional Array Help

Post by hallsofvallhalla »

you are using PHP? I am using javascript above.
Post Reply

Return to “Beginner Help and Support”