如何使用php删除json输出中的“[]”

how can i convert the following json format

[
    [{
        "product_id": 1,
        "name": "Cotton Shirt",
        "sku": "abcdef"
    }],
    [{
        "product_id": 2,
        "name": "Silk Shirt",
        "sku": "csfddef"
    }]
]

to like below

[{
    "product_id": 1,
    "name": "Cotton Shirt",
    "sku": "abcdef"
}, {
    "product_id": 2,
    "name": "Silk Shirt",
    "sku": "csfddef"
}]

I just want to remove the extra square bracket.How it is possible

Use substr() to return from the first character to the second last character:

$input = '[[{"product_id":1,"name":"Cotton Shirt","sku":"abcdef"}]]';
$result = substr($input, 1, -1);

// $result: [{"product_id":1,"name":"Cotton Shirt","sku":"abcdef"}]

If the JSON is already an array, then just get the first item:

$result = json_encode($input[0]);

// $result: [{"product_id":1,"name":"Cotton Shirt","sku":"abcdef"}]

Try this:

<?php

$json = '[[{"product_id":1,"name":"Cotton Shirt","sku":"abcdef"}],[{"product_id":2,"name":"Silk Shirt","sku":"csfddef"}]]';

$arrayData = json_decode($json, true);

$newData = array_map(function($data) {return $data[0];}, $arrayData);
$json = json_encode($newData);

print_r($json);

Result is:

[{"product_id":1,"name":"Cotton Shirt","sku":"abcdef"},{"product_id":2,"name":"Silk Shirt","sku":"csfddef"}]

It works, check here: CLICK!

If you are not able to fetch the internal array from the initial one beforehand, try the following approach using json_decode and json_encode functions:

$arr = json_decode('[[{"product_id":1,"name":"Cotton Shirt","sku":"abcdef"}]]')[0];
print_r(json_encode($arr));  // "[{"product_id":1,"name":"Cotton Shirt","sku":"abcdef"}]"