PHP : transform a 2 dimension array into 1 dimension
Is there any PHP function that can be used in my case ?
Cur开发者_JAVA技巧rently I have this array :
array( "1" => "22", "2" => "4", "3" => "0" );
And I need to keep the values and not the keys :
EDIT (array('votes' => array("22","4","0"));
I've seen that there is the array_values
but it retunrs a 2 dimensional array.
Thanks
This is not a two dimensional array, it's a one dimensional hash/map. array_values()
returns a one dimensional array as well, but it's map is 0 => 22, 1 => 4, 3 => 0, etc. It can be treated like a one dimensional array.
By the way, if you need to ignore the keys for the purposes of iteration, you don't need to use array_values()
anyway.
foreach (array( "1" => "22", "2" => "4", "3" => "0" ) as $_) {
echo "$_\n";
//22
//4
//0
}
PHP arrays are always at least "2d". A key and a value. Even your second "desired" array will still have keys in there. It's impossible to have an array with just keys or just values.
精彩评论