Please tell me how you can delete the values ​​in the array leaving the keys, without any cycles.

For example, there is such an array:

$array = [ 'one' => 1, 'two' => 2, ... ]; 

Convert it to this:

 $array = [ 'one', 'two', ... ]; 

  • 2
  • Well, it returns $ array = [0 => 'one', 1 => 'two', ...]; And I need no values, only the keys - Ololosha
  • @Olosha you to read php.net. Arrays are only associative - as in the first example, and numbered '$ array = [0 =>' one ', 1 =>' two ', ...]' And nothing else. Perhaps it would be better to describe the goal that you want to achieve with this behavior - Vlad
  • Get separated! echo json_encode(array_keys($array)); - vp_arth
  • As I understand it, you need to clear the array of values. Then array_fill_keys(array_flip($array),''); - Ans

1 answer 1

You can use the function array_keys($array)

  array_keys($array) 

Get from the array, only qiq: array_map

 $array = [ 'one' => 1, 'two' => 2 ]; $myarr = array_map(create_function('$n', 'return null;'), $array); 
  • Returns $ array = [0 => 'one', 1 => 'two', ...]; and I need $ array = ['one', 'two', ...]; - Ololosha
  • @Ololosha Trick you want $array = [ 'one', 'two' ]; print_r($array); $array = [ 'one', 'two' ]; print_r($array); returns Array ( [0] => one [1] => two ) because it is the same. There are no arrays without keys in nature. ideone.com/jEiMyR - Mike
  • I updated the code, now it works - L. Vadim
  • one
    Why spoil a good answer - I do not understand. - Ipatiev
  • @ Ipatiev Why spoil, the man did not get the expected answer, I corrected - L. Vadim