473,320 Members | 1,865 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to walk trough 2d array in ONE loop?

I have to print all values with one loop (IT DOESN'T NEED print_r[$array])
this is what i made til now but it isn't working properly
Expand|Select|Wrap|Line Numbers
  1. $array = array(
  2.         array(11,12,13,14,15,16),
  3.         array(21,22,23,24,25,26),
  4.         array(31,32,33,34,35,36),
  5.         array(41,42,43,44,45,46),
  6.         array(51,52,53,54,55,56),
  7.         array(61,62,63,64,65,66)
  8. );
  9.  
  10.  
  11. $colindex = 0;
  12. $rowindex = 0;
  13. for ($i = 0; $i<35; $i++)
  14. {
  15.  
  16.  
  17.     if($rowindex < 5)
  18.     {
  19.  
  20.             if ($colindex < 5)
  21.             {
  22.                 $sumindexes = $colindex + $rowindex;
  23.  
  24.             }
  25.             else 
  26.             {
  27.                 $colindex = 0;
  28.             }
  29.             $colindex++;
  30.             $rowindex++;
  31.     }
  32.  
  33.     else 
  34.     {
  35.         $rowindex = 0;
  36.  
  37.     }
  38.  
  39.     print_r($array[$rowindex][$colindex]);
  40.  
  41. }
  42.  
Nov 20 '15 #1
2 1131
Rabbit
12,516 Expert Mod 8TB
1) Why make it difficult on yourself by using only one loop when 2 loops work just as well?

2) You're calculating the row and column indexes incorrectly.

3) It's much easier to calculate the indexes using integer division for one and mod for the other.
Nov 20 '15 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Bob Bedford | last post by:
I've this structure in a XML file: Type Model Detail Datas Datas Datas Datas /Detail Detail
0
by: Charles Alexander | last post by:
Hello I am new to php & MySQL - I am trying to retrieve some records from a MySQL table and redisplay them. The data in list form looks like this: Sample_ID Marker_ID Variation ...
3
by: Craig Keightley | last post by:
I have the following form <FORM NAME="form1" METHOD="POST"> <?php do { ?> <input name="approve" type="checkbox" id="approve" value="<?php echo $row_rs; ?>"> <select name="select"> <option...
2
by: Bob Bedford | last post by:
I've a directory structure where I store images. 0-999: images/0/ then images/0/0/ then images/0/0/0-1.jpg images/0/0/0-2.jpg images/0/0/0-3.jpg
1
by: Limpor | last post by:
Hi there, I’m working on a freeCell homework, there are 8 piles in the tableau (which is an array of Pile objects), and i need go through a loop, and take a card from the deck , and put it into...
6
by: Amy | last post by:
Hello, I have an array with 60 items in it, one for every second, But when it gets to the end of the 60 items it stops. How do I get it to start over when it gets to the end of the array? Please...
2
by: phattymatty | last post by:
I am having trouble getting this loop to do what I would like it to. I need to display information from a multidimensional array in order. The order has already been sorted in the array using a...
2
by: Cainnech | last post by:
Hi guys, It's me again. Arrays are a mindbuster for me. I'm stuck again but I can't see the error. <HTML> <HEAD> <SCRIPT> var lotto = ; var winst = ;
3
by: eDaddi | last post by:
I can't figure out why I cant get this simple loop to work. I'm using it to validate a form. I thought I could put the required field names in an array, loop through the array and have the it check...
2
by: Sjoerd | last post by:
On Mon, 08 Sep 2008 00:11:38 -0700, mouac01@yahoo.com wrote: Loop through the array and make an array of all keys. Then loop through the array again and add the missing keys.
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.