Jose Browne
Jose Browne

Reputation: 4632

Get just the values from an associative array in 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?

Upvotes: 8

Views: 22254

Answers (1)

vstm
vstm

Reputation: 12537

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.

Upvotes: 16

Related Questions