How to remove spaces in array keys names in php?

前端 未结 3 2019
你的背包
你的背包 2020-12-20 02:08

I am trying to remove all spaces in array keys names i.e. str_replace(\' \',\'\',$value) (or worst cast scenario replace them with underscores (_) )

and I am trying

3条回答
  •  醉梦人生
    2020-12-20 02:49

    You can pass an array to str_replace so it's much cleaner and easier to just do this:

    $my_array = array( 'one 1' => '1', 'two 2' => '2' );
    $keys = str_replace( ' ', '', array_keys( $my_array ) );
    $results = array_combine( $keys, array_values( $my_array ) );
    

    Result:

    array(2) {
      ["one1"]=>
      string(1) "1"
      ["two2"]=>
      string(1) "2"
    }
    

    Example: https://glot.io/snippets/ejej1chzg3

提交回复
热议问题