json 字符串的多级 php 数组。输出错误

Multilevel php array for json string. Output wrong

我尝试使用我可以在 JSON 中编码的 DB 连接生成多级 PHP 数组。 输出有一个方括号对,我想删除很多。

我有以下 php-code

## Step 1 #################
    do{
    $tabContent[] = array
        (
            "name" => $row_profile['name'],
            "titel" => $row_profile['titel']
        );
}while ($row_profile = $statement_profile->fetch(PDO::FETCH_ASSOC));

## Step 2 #################
$tabelled['repositories'] = array 
    (
        $tabContent
    );


## Step 3 #################
header('Content-Type: application/json');
echo json_encode($tabelled, JSON_PRETTY_PRINT);

所以现在我有以下回复:

{
    "repositories": [
        [
            {
                "name": "test",
                "titel": "a sdfaf"
            },
            {
                "name": "Frank wieder",
                "titel": "test test"
            }
        ]
    ]
}

但我需要以下输出

{
  "repositories": [
    {
        "name": "test",
        "titel": "a sdfaf"
    },
    {
        "name": "Frank wieder",
        "titel": "test test"
    }
  ]
}

所以错误在步骤 1 变量 $tabContent 之后的方括号中。但是没有我就不能制作多级数组。 我该怎么做才能删除 "repositories" 之后的第二个方括号?

您的 $tabContent 已经是一个数组,您将它包装在一个额外的数组中。没有那个,它应该按预期出现。

$tabelled['repositories'] = $tabContent;