1

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"?

1 Answer 1

1

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;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.