Get only values ​​from associative array in php

I have a simple array, for example:

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

Desired Result:

  array (101, 105, 103)

Is it possible?

+4
source share
1 answer

Yes, use array_values .

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

Edit: (Thanks @MarkBaker)

If you use var_dump in the source array and in the "only values" array, then the output may look exactly the same if the keys are numeric and ascending, starting with 0. As in your example.

If the keys do not consist of numbers or if the numbers are "random", then the output will be different. For example, if the array looks like

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

var_dump output looks different after converting an array with array_values .

+7
source

Source: https://habr.com/ru/post/1387008/


All Articles