O
O
Orc2020-05-25 13:52:26
PHP
Orc, 2020-05-25 13:52:26

How to convert JSON object in a loop using PHP?

Tell me how to convert a JSON object of this kind:

{
  "1166E523-2F40-65BC-1149-71449B66E9D8":{
    "time_stamp":1590396226,
    "public_key":"123",
    "private_key":"123"
  },
  "6C1D4749-55AF-67B9-E01C-166391757836":{
    "time_stamp":1590396356,
    "public_key":"123",
    "private_key":"123"
  },
  "001B478F-7441-A951-E7EA-C4973F6959E0":{
    "time_stamp":1590396393,
    "public_key":"123",
    "private_key":"123"
  },
  "BD05974F-5B35-3B7E-95ED-520F6429B46C":{
    "time_stamp":1590397179,
    "public_key":"123",
    "private_key":"123"
  }
}


in JSON - an object like this:
{
  "total": 4,
  "totalNotFiltered": 4,
  "rows": [
    {
      "uid":"1166E523-2F40-65BC-1149-71449B66E9D8",
      "time_stamp":1590396226,
      "public_key":"123",
      "private_key":"123"
    },
    {
      "uid":"6C1D4749-55AF-67B9-E01C-166391757836",
      "time_stamp":1590396356,
      "public_key":"123",
      "private_key":"123"
    },
    {
      "uid":"001B478F-7441-A951-E7EA-C4973F6959E0",
      "time_stamp":1590396393,
      "public_key":"123",
      "private_key":"123"
    },
    {
      "uid":"BD05974F-5B35-3B7E-95ED-520F6429B46C",
      "time_stamp":1590397179,
      "public_key":"123",
      "private_key":"123"
    }
  ]
}


where total and totalNotFiltered are equal to the number of rows in the selection

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2020-05-25
@z0ddak

$arr = json_decode($json, true);
$newJson = json_encode([
  'total' => count($arr),
  'totalNotFiltered ' => count($arr),
  'rows' => array_map(
    function ($uid, $el) {
      $el['uid'] = $uid;
      return $el;
    },
    array_keys($arr),
    array_values($arr)
  )
]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question