Jump to content

Array key re arrange


fortnox007

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!  ::)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.