简体   繁体   中英

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?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM