fortnox007 Posted October 30, 2010 Share Posted October 30, 2010 Hi all, I am trying to fix some easy script that explodes a string into an array. Which goes fine. The only problem i have is that if i echo out the Key of the array it starts of course by default with 0 Does anyone know a quick may to rename the keys and start with 1 instead of 0. so to make it more clear: Value: Key: | | Value: Key: ------------------------------------------------------------------------------------------- johny 0 | | johny 1 willy 1 | should look like => | Willy 2 monkey 2 | | monkey 3 adrian 3 | | adrian 4 ------------------------------------------------------------------------------------------- This a small part of what i have which works but of course starts with index 0 <?php $string = array('johny','willy','monkey','adrian'); print_r($string); ?> Just a small side note: This is a string that catches input from a text-area and explodes it based on a newline character. But for the sake of simplicity i provided a ready made array. But I was wondering if someone knows a nice and clean way to re-arrange the key of the array (which the user has no influence on). I was thinking of a loop and increment the array key, but that might be redundant. Any hints or help are appreceated Quote Link to comment https://forums.phpfreaks.com/topic/217315-array-key-re-arrange/ Share on other sites More sharing options...
PaulRyan Posted October 30, 2010 Share Posted October 30, 2010 <?php $string = array(1=> 'johny','willy','monkey','adrian'); print_r($string); ?> That is how you start it at the number 1, by simply naming the key Hopefully that will help bud. Regards, Paul. Quote Link to comment https://forums.phpfreaks.com/topic/217315-array-key-re-arrange/#findComment-1128453 Share on other sites More sharing options...
BlueSkyIS Posted October 30, 2010 Share Posted October 30, 2010 curious why you need to increment the array key? one way, something like this: $num_elems = count($somearray); for ($i=$num_elems - 1;$i>0;$i--) { $somearray[$i + 1] = $somearray[$i]; } $somearray[0] = ''; I assumed that the array was already created starting at 0. I didn't realize you could start with a different number by simply defining that key on the first element. neat. Quote Link to comment https://forums.phpfreaks.com/topic/217315-array-key-re-arrange/#findComment-1128455 Share on other sites More sharing options...
PaulRyan Posted October 30, 2010 Share Posted October 30, 2010 Well seems like you learn something new everyday huh? Quote Link to comment https://forums.phpfreaks.com/topic/217315-array-key-re-arrange/#findComment-1128459 Share on other sites More sharing options...
salathe Posted October 30, 2010 Share Posted October 30, 2010 Why do you want to start the key at 1? If it's a purely display thing, then just add one when printing the number. Quote Link to comment https://forums.phpfreaks.com/topic/217315-array-key-re-arrange/#findComment-1128475 Share on other sites More sharing options...
fortnox007 Posted October 31, 2010 Author Share Posted October 31, 2010 Hi all, This sure is a meeting between the gods except for me that is . I got more than i asked for. For which i am very grateful. It was meant purely for a visual effect, (i.e. someone wins a lotery its nicer to say your number 1 instead of number 0 ) but I have now seen a very cool trick thanks to Paul Ryan and got a confirm on the for loop which I was thinking of by bluesky (which my brains didn't process yet). Awesome thanks all! They should build you all a statue! Quote Link to comment https://forums.phpfreaks.com/topic/217315-array-key-re-arrange/#findComment-1128518 Share on other sites More sharing options...
fortnox007 Posted October 31, 2010 Author Share Posted October 31, 2010 Hmm i tested a bit an the code Paul Ryan provided sure is a awesome. The only thing I am facing now is how to handle an already existing array. Like $_POST or $_GET What I have is: $dirty = preg_split("~\n~",trim($_POST['text_area_data']),-1); which fetches POST data and splits it on a newline. For some reason (but maybe that's because it's saturday night ) I can't see how to assign the key in here like PAul showed. anyone have an idea? If i am correct $dirty now is an array. Quote Link to comment https://forums.phpfreaks.com/topic/217315-array-key-re-arrange/#findComment-1128536 Share on other sites More sharing options...
BlueSkyIS Posted October 31, 2010 Share Posted October 31, 2010 The only thing I am facing now is how to handle an already existing array. Like $_POST or $_GET That is a good example of why you should not increase the index (key) of the arrays. Know that the first index is 0. If you need to display "1", "2", "3", etc., add 1 to each index at display time. But by convention, the first index of an array is 0. On the other hand, you might use code like I posted before to increment every index. Quote Link to comment https://forums.phpfreaks.com/topic/217315-array-key-re-arrange/#findComment-1128550 Share on other sites More sharing options...
fortnox007 Posted October 31, 2010 Author Share Posted October 31, 2010 The only thing I am facing now is how to handle an already existing array. Like $_POST or $_GET That is a good example of why you should not increase the index (key) of the arrays. Know that the first index is 0. If you need to display "1", "2", "3", etc., add 1 to each index at display time. But by convention, the first index of an array is 0. On the other hand, you might use code like I posted before to increment every index. Thanks a lot bluesky I'll sure keep that in mind. Quote Link to comment https://forums.phpfreaks.com/topic/217315-array-key-re-arrange/#findComment-1128621 Share on other sites More sharing options...
fortnox007 Posted October 31, 2010 Author Share Posted October 31, 2010 For anyone that might want have the same result without ruining the index. here is what i did. I hope someone can use it. But i am very happy with the extra tips and hints above foreach($clean as $key => $value){ $key2 = $key + 1;// add 1 to make a nicer looking list starting at 1 instead of 0 if ($key2 == 1){$jackpot = '(Jackpot!!)';}else{$jackpot = '';} // as an extra to tell place 1 is the winner echo "<tr> <td>{$value}$jackpot</td> <td>{$key2}</td> </tr>"; } This is part of a table with 2 columns Cheers and thanks guys above! Quote Link to comment https://forums.phpfreaks.com/topic/217315-array-key-re-arrange/#findComment-1128638 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.