Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to remove Square Bracket from JSON in JavaScript or in php?

I have some JSON data, it is apairing like this now

[
  [
    {
      'key': 'value'
    },
    {
      'key': 'value'
    }
  ]
]

What I want to do, is remove the second square bracket from here like this:

[
  {
    'key': 'value'
  },
  {
    'key': 'value'
  }
]

I’ve applied some algorithms in both PHP and javascript but it does not work
please help me if you know how to solve this

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use the array_merge() PHP method like this (PHP 7.4 or above)

EDIT:
Decode the JSON string using json_decode() php function before.

$flattened_array = array_merge(...$arr);

https://wiki.php.net/rfc/spread_operator_for_array

https://www.php.net/manual/pt_BR/function.array-merge.php

Or simply do this:

$arr = "[
  [
    {
      'key': 'value'
    },
    {
      'key': 'value'
    }
  ]
]" //receives a JSON string;

$flattened_arr = json_decode($arr[0]) //decodes it;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading