levidyllan Posted June 19, 2007 Share Posted June 19, 2007 hi! I have the following two arrays and then what I put in them, just wondering if there is a better way ? The arrays $dec =array(); $deca =array(); then adding to the arrays: if($row['events_month']=="December"){ $dec[]=$row['events_title']; $deca[]=$row['events_id']; }// end if so could I create a double array and have it enter the info into this reducing the need for two arrays, something like: $dec=array(); if($row['events_month']=="December"){ $dec[]=($row['events_title'], $row['events_id']); }// end if So it could be called like "echo 'title: ' .$dec[1,0] . ' code: ' . $dec[1,0]; etc etc this is how I display it at the moment. if ($decCount > 0){ ?> <h2><span class="monlink"><a href="events.php?month=December" onclick="document.getElementById('decDiv').style.display = ''; return false;">December </a></span><span class="entries">(<?php echo $decCount ?>)</span></h2> <?php echo'<div id="decDiv" style="display:none;">'; for($i=0; $i<=$decCount; $i++){ echo '<h3><a href="events.php?event='.$deca[$i].'&month=Decemeber">' . $dec[$i] . '</a></h3>'; } echo '</div>'; } Quote Link to comment https://forums.phpfreaks.com/topic/56238-simplification/ Share on other sites More sharing options...
akitchin Posted June 19, 2007 Share Posted June 19, 2007 they're called multidimensional arrays - an array of arrays: $dec[] = array($row['events_title'], $row['events_id']); you can then access each as: $dec[0][0] is $row['events_title'], $dec[0][1] is $row['events_id'] Quote Link to comment https://forums.phpfreaks.com/topic/56238-simplification/#findComment-277772 Share on other sites More sharing options...
levidyllan Posted June 19, 2007 Author Share Posted June 19, 2007 thats it [][] simple things you forget easily, multidimensional not double doh! cheers will give it ago Quote Link to comment https://forums.phpfreaks.com/topic/56238-simplification/#findComment-277794 Share on other sites More sharing options...
cooldude832 Posted June 19, 2007 Share Posted June 19, 2007 as of php 4, i believe, you do not need to initialize arrays like that, all you need is n represents an integer value $data[n]['field1'] = "blah"; $data[n]['field2'] = "blah blah"; you don't need to initaliize the array at all just start declaring values. Quote Link to comment https://forums.phpfreaks.com/topic/56238-simplification/#findComment-277800 Share on other sites More sharing options...
levidyllan Posted June 19, 2007 Author Share Posted June 19, 2007 so I can have: $dec[n]['month'] = $row['events_title']; $dec[n]['id'] = $row['events_id']; ?:-) Quote Link to comment https://forums.phpfreaks.com/topic/56238-simplification/#findComment-277810 Share on other sites More sharing options...
cooldude832 Posted June 19, 2007 Share Posted June 19, 2007 yeah, i'm not saying my method is the right, but for me arrays are easier in multi deminsonial using that "n" as the key value linking them. You can also perform sorts on the array based on its subset values. Quote Link to comment https://forums.phpfreaks.com/topic/56238-simplification/#findComment-277812 Share on other sites More sharing options...
levidyllan Posted June 19, 2007 Author Share Posted June 19, 2007 but how would I combine this into my code where at the mo i would have : $jan[] = array($row['events_title'], $row['events_id']); $feb[] = array($row['events_title'], $row['events_id']); $mar[] = array($row['events_title'], $row['events_id']); ...etc etc so how would I display this version then: simply put "echo $dec[1]['month'];" etc Quote Link to comment https://forums.phpfreaks.com/topic/56238-simplification/#findComment-277826 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.