amg182 Posted September 24, 2011 Share Posted September 24, 2011 Hi. I have an multidemesional array that looks like this: $numbers=array("1"=>'under 5',"2"=>'under 5',"3"=>'under 5',"4"=>'under 5'); Is it possible to write the array like this?(without using loops) $numbers=array("1-4"=>'under 5'); or even: $numbers=array("1,2,3,4"=>'under 5'); Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/247781-array-help/ Share on other sites More sharing options...
jcbones Posted September 24, 2011 Share Posted September 24, 2011 That isn't a multi-dem array, but rather a standard array. No, you can't write an array like that, but you can: <?php $numbers = array_fill(1,4,'under 5'); echo '<pre>' . print_r($numbers,true) . '</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/247781-array-help/#findComment-1272362 Share on other sites More sharing options...
amg182 Posted September 24, 2011 Author Share Posted September 24, 2011 Thanks for reply, worked a treat. How could i add this to the array ? (5,10,'under 10') Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/247781-array-help/#findComment-1272372 Share on other sites More sharing options...
Pikachu2000 Posted September 24, 2011 Share Posted September 24, 2011 Although it isn't completely clear what you're trying to do, on the surface this doesn't look like the best way to do it. What are you attempting to achieve with this? Quote Link to comment https://forums.phpfreaks.com/topic/247781-array-help/#findComment-1272373 Share on other sites More sharing options...
the182guy Posted September 24, 2011 Share Posted September 24, 2011 Are you trying to combine arrays on the fly? Quote Link to comment https://forums.phpfreaks.com/topic/247781-array-help/#findComment-1272382 Share on other sites More sharing options...
amg182 Posted September 24, 2011 Author Share Posted September 24, 2011 I am trying to echo out a particular comment depending on what is entered witihn a form(text input). So if someone enters a value between 1-1000 it would echo out "Under 1000". The problem i have is that i users can enter a value between 0 - 100,000. Ideally i would like the text echo out to the nearest thousand so if someone entered 5400 it would echo out "under 6000". Any ideas? Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/247781-array-help/#findComment-1272424 Share on other sites More sharing options...
Pikachu2000 Posted September 24, 2011 Share Posted September 24, 2011 That can be done with a simple calculation. No need to go through the hassle of putting all the values in an array. This should get you on the right track. $val = 65432; $m = ceil($val/1000); echo "Less than $m,000"; // Returns "Less than 66,000" Quote Link to comment https://forums.phpfreaks.com/topic/247781-array-help/#findComment-1272430 Share on other sites More sharing options...
amg182 Posted September 24, 2011 Author Share Posted September 24, 2011 Thanks Pikachu2000. This is much more effecient! I was going about this totally wrong. Oh well, its a learning curve...as you can see i have a lot to learn. Thanks again! A credit to the forum. Quote Link to comment https://forums.phpfreaks.com/topic/247781-array-help/#findComment-1272431 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.