0

Is there any PHP function that can be used in my case ?

Currently 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

1
  • array_values most certainly does not return a 2 dimensional array. It's as 1-dimensional as a PHP array gets; that is, it had numeric sequential keys starting at 0. Commented Feb 17, 2011 at 14:36

2 Answers 2

4

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
}
Sign up to request clarification or add additional context in comments.

Comments

2

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.

2 Comments

Oh sorry for the confusion. Here's what i want to output : (array('votes' => array("22","4","0"))
That's easy enough, you could just do $myarray['votes'] = array(22, 4, 0);.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.