tags:

views:

185

answers:

3

I sometimes end up having data in an array that starts far into the array, at position 25 instead of 0 for example.

Example:

Array
(
    [16] => Array
        (
            [0] => http://rapidshare.com/files/268123830/hmh.fo3-oks.part01.rar
            [1] => Marked as illegal
        )

    [17] => Array
        (
            [0] => http://rapidshare.com/files/268124204/hmh.fo3-oks.part02.rar
            [1] => Marked as illegal
        )

    [18] => Array
        (
            [0] => http://rapidshare.com/files/268127882/hmh.fo3-oks.part03.rar
            [1] => Marked as illegal
        )
)

This is because of the user input, not my coding. I need a way to clean up the array to somehow make it 0 based again. The above example should be like this after the cleanup:

Array
(
    [0] => Array
        (
            [0] => http://rapidshare.com/files/268123830/hmh.fo3-oks.part01.rar
            [1] => Marked as illegal
        )

    [1] => Array
        (
            [0] => http://rapidshare.com/files/268124204/hmh.fo3-oks.part02.rar
            [1] => Marked as illegal
        )

    [2] => Array
        (
            [0] => http://rapidshare.com/files/268127882/hmh.fo3-oks.part03.rar
            [1] => Marked as illegal
        )
)

So I can effectively loop though each array element and output it to the user.

Any help on how I would clean up this array would be useful, thanks. :)

+3  A: 

Use array_values(). It discards all the keys and returns a zero-based array while preserving the relative order of the items.

Emil H
+1  A: 

You could use array_values to get the values of the array, with keys that'll start at 0 :

array array_values  ( array $input  )

array_values() returns all the values from the input array and indexes numerically the array.

In your case, for instance :

$a = array(
    16 => array(
        'http://rapidshare.com/files/268123830/hmh.fo3-oks.part01.rar', 
        'Marked as illegal'
    ),
    17 => array(
        'http://rapidshare.com/files/268123830/hmh.fo3-oks.part02.rar', 
        'Marked as illegal'
    ),
    18 => array(
        'http://rapidshare.com/files/268123830/hmh.fo3-oks.part03.rar', 
        'Marked as illegal'
    ),
);
$b = array_values($a);
var_dump($b);

Will get you :

array
  0 => 
    array
      0 => string 'http://rapidshare.com/files/268123830/hmh.fo3-oks.part01.rar' (length=60)
      1 => string 'Marked as illegal' (length=17)
  1 => 
    array
      0 => string 'http://rapidshare.com/files/268123830/hmh.fo3-oks.part02.rar' (length=60)
      1 => string 'Marked as illegal' (length=17)
  2 => 
    array
      0 => string 'http://rapidshare.com/files/268123830/hmh.fo3-oks.part03.rar' (length=60)
      1 => string 'Marked as illegal' (length=17)
Pascal MARTIN
Thank you. I seemed to remember a function that does what I want when I was thinking about this problem but I couldn't find it on php.net. Thanks again :)
Joseph
You're welcome :-) Have fun !
Pascal MARTIN
A: 

array_values() is the correct answer to your exact question as provided by the other answers, but you do not need to do this to simply loop and output to the user. Just use a foreach:

<?php foreach($your_array as $item): ?>
    <div class='item'>
    <?php echo $item[0] ?>
    </div>
<?php endforeach; ?>
Doug Neiner