D
D
di2020-05-19 14:29:06
PHP
di, 2020-05-19 14:29:06

How to recursively replace spaces in array keys with underscores?

There is an array with dynamic data.
Added like this.

$item->characteristics[$v->characteristic->name] = $v->value;

I convert it to json and get it.
[
  {
    "id": "o_164",
    "created_at": "2010-02-16 15:12:00",
    "items": [
      {
        "name": "Стул ()",
        "price": 2170,
        "quantity": 4,
        "uid": "",
        "characteristics": {
          "Раскладной": "нет",
          "Жесткость стульев": "Мягкие стулья",
          "Ширина c пробелом": 440
        }
      }
    ]
  }
]

So you need to replace the spaces in the json keys with "_". i.e. instead of "Width with space" "Width_with_space". And so on all array to replace keys. How to do it competently and efficiently?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question