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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With