I try to generate a multilevel PHP Array with a DB-Connection that i can encode in JSON. The output have an square-bracket-pair to much that i want to remove.
I have following 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);
So now i have the following response:
{
"repositories": [
[
{
"name": "test",
"titel": "a sdfaf"
},
{
"name": "Frank wieder",
"titel": "test test"
}
]
]
}
but i need following output
{
"repositories": [
{
"name": "test",
"titel": "a sdfaf"
},
{
"name": "Frank wieder",
"titel": "test test"
}
]
}
So the error is on step 1 the square bracket after the variable $tabContent. But without i cannot make the multilevel array. What can i do, to remove the second square bracket after "repositories"?
Your $tabContent is already an array, and you're wrapping it inside of an extra array. Without that, it should come out as expected.
$tabelled['repositories'] = $tabContent;