Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get just the values from an associative array in php

Tags:

arrays

php

I have a simple array like the following:

    array
      0 => string '101'
      1 => string '105'
      2 => string '103'

Desired result:

    array(101, 105, 103)

Is this possible?

like image 372
Jose Browne Avatar asked Dec 19 '11 09:12

Jose Browne


1 Answers

Yes, use array_values.

array_values(array('0' => '101', '1' => '105', '2' => '103')); // returns array(101, 105, 103)

Edit: (Thanks to @MarkBaker)

If you use var_dump on the original array and the "values only" array the output might look exactly the same if the keys are numerical and and ascending beginning from 0. Just like in your example.

If the keys are not consisting of numbers or if the numbers are "random" then the output would be different. For example if the array looks like

array('one' => '101', 'two' => '105', 'three' => '103')

the output of var_dump looks different after converting the array with array_values.

like image 98
vstm Avatar answered Nov 04 '22 22:11

vstm