Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cleaning up a PHP array to make it 0 based again

Tags:

arrays

php

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

like image 992
Joseph Avatar asked Jan 22 '23 22:01

Joseph


2 Answers

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

like image 116
Emil H Avatar answered Jan 25 '23 12:01

Emil H


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)
like image 21
Pascal MARTIN Avatar answered Jan 25 '23 10:01

Pascal MARTIN