简体   繁体   中英

Cleaning up a PHP array to make it 0 based again

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

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

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)

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; ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM