grantp22 Posted August 10, 2010 Share Posted August 10, 2010 Hi can anybody help me out with the following problem I am having? I need to populate the key values in the multi-dimentional array structure shown below and the values will be used later in the following way: <tr> <td><?php echo $sm_array[1]['col_1']; ?></td> <td><?php echo $sm_array[1]['col_2']; ?></td> <td><?php echo $sm_array[1]['col_3']; ?></td> <td><?php echo $sm_array[1]['col_4']; ?></td> //and so on, up to 10 </tr> <tr> <td><?php echo $sm_array[2]['col_1']; ?></td> <td><?php echo $sm_array[2]['col_2']; ?></td> <td><?php echo $sm_array[2]['col_3']; ?></td> <td><?php echo $sm_array[2]['col_4']; ?></td> //and so on, up to 10 </tr> This is the array structure, $td->innertext is just a different value each time and is not important for this example, it could essentially be anything: sm_array[$row] = array( "col_1" => $td->innertext, "col_2" => $td->innertext, "col_3" => $td->innertext, "col_4" => $td->innertext, "col_5" => $td->innertext, "col_6" => $td->innertext, "col_7" => $td->innertext, "col_8" => $td->innertext, "col_9" => $td->innertext, "col_10" => $td->innertext, ); But I need to add the values inside a loop, like this: while($row=0; $row<=10; $row++) { $sm_array[$row] = array( "col_".$row => $td->innertext); } But this doesn't allow me to change the key name inside the loop, so for $sm_array[1] there will be 10 keys and their values, and the same for $sm_array[2], it will also have 10 keys and different values for each key and so on! But for some reason the key value will not work this way inside a loop, it doesn't allow "col_".$row and I even tried to echo the key name, like this: while($row=0; $row<=10; $row++) { $sm_array[$row] = array( "<?php echo $row; ?>" => $td->innertext); } It only seems to accept a fixed string or integer as the key, like this: while($row=0; $row<=10; $row++) { $sm_array[$row] = array( "col_1" => $td->innertext); // or like this $sm_array[$row] = array( 1 => $td->innertext); } But then I have a problem, I can't change the key name dynamically! I have tried doing this with conditional staments like this, but it only accepts the first key name and the 2nd, 3rd etc, etc all fail: while($row=0; $row<=10; $row++) { if($row==1){ $sm_array[$row] = array( "col_1" => $td->innertext); }else if($row==2){ $sm_array[$row] = array( "col_2" => $td->innertext); } //and so on } Can somebody please tell me what I am doing wrong or can't this be done or is their some other way of doing this? Thanks in advance Grant Quote Link to comment https://forums.phpfreaks.com/topic/210270-multi-dimentional-array-how-to/ Share on other sites More sharing options...
DavidAM Posted August 10, 2010 Share Posted August 10, 2010 Close. Try: while($row=0; $row<=10; $row++) { for ($col = 1; $col <= 10; $col++) { $key = 'col_' . $col; $sm_array[$row][$key] = $td->innertext; } } Quote Link to comment https://forums.phpfreaks.com/topic/210270-multi-dimentional-array-how-to/#findComment-1097270 Share on other sites More sharing options...
grantp22 Posted August 10, 2010 Author Share Posted August 10, 2010 Thanks for the suggestion DavidAM, You would expect that to work, right, but it doesn't! Below is a variation of your solution, and it too doesn't work, nothing gets echoed, just a blank screen! <?php $n = array(1 => "aa", 2 => "ab", 3 => "ac", 4 => "ad", 5 => "ae", 6 => "af", 7 => "ag", 8 => "ah", 9 => "aj", 10 => "ak"); $row = 1; $col = 1; while($row<=10) { while($col<=10) { $key = 'col_' . $col; $sm_array[$row][$key] = $n[$col]; $col++; } $row++; } echo sm_array[1]['col_1']; ?> Does anybody have any ideas why this is not working? Quote Link to comment https://forums.phpfreaks.com/topic/210270-multi-dimentional-array-how-to/#findComment-1097406 Share on other sites More sharing options...
Barand Posted August 10, 2010 Share Posted August 10, 2010 Because there is a $ missing from the front of your array name in the final echo echo sm_array[1]['col_1']; Quote Link to comment https://forums.phpfreaks.com/topic/210270-multi-dimentional-array-how-to/#findComment-1097512 Share on other sites More sharing options...
grantp22 Posted August 10, 2010 Author Share Posted August 10, 2010 Thanks Barand, I have already been made aware of this a few minutes ago on another forum, I can't believe I missed that! It's normally one of the first things I check... Quote Link to comment https://forums.phpfreaks.com/topic/210270-multi-dimentional-array-how-to/#findComment-1097684 Share on other sites More sharing options...
Barand Posted August 11, 2010 Share Posted August 11, 2010 You also need to reset $col each time round the outer loop ... while($row<=10) { $col = 1; // reset here while () { ... Quote Link to comment https://forums.phpfreaks.com/topic/210270-multi-dimentional-array-how-to/#findComment-1097974 Share on other sites More sharing options...
grantp22 Posted August 22, 2010 Author Share Posted August 22, 2010 I agree, in this example you would need to reset $col, but for my code, these two loops are actually inside another loop which I never included just to simplify things! Thanks for the help Barend! Quote Link to comment https://forums.phpfreaks.com/topic/210270-multi-dimentional-array-how-to/#findComment-1102333 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.