0

Hi guys im having some trouble creating the json i need to send to my datatable JS. I see that in my output i get the ''keys'' inside the array and in the one that i need i dont get them. How can i creat an array without those keys or i dont need an array inside another array? So this is the output of the JSON that i need:

{
    "data": [{
            "DT_RowId": "row_1",
            "first_name": "Tiger",
            "last_name": "Nixon",
            "position": "System Architect",
            "email": "[email protected]",
            "office": "Edinburgh",
            "extn": 5421,
            "age": 61,
            "salary": 20800,
            "start_date": "2011-04-25"
        },
        {
            "DT_RowId": "row_1",
            "first_name": "Tiger",
            "last_name": "Nixon",
            "position": "System Architect",
            "email": "[email protected]",
            "office": "Edinburgh",
            "extn": 5421,
            "age": 61,
            "salary": 20800,
            "start_date": "2011-04-25"
        }
    ]
}

what i have is this:

$json = array(
        'Data' => array(
                '1' => array(
                        "DT_RowId" => "row_1",
                        "first_name"=> "Tiger",
                        "last_name"=>  "Nixon",
                        "position"=>  "System Architect",
                        "email"=>  "[email protected]",
                        "office"=>  "Edinburgh",
                        "extn"=>  5421,
                        "age"=>  61,
                        "salary"=>  20800,
                        "start_date"=>  "2011-04-25"
                ),
                '2' => array(
                        "DT_RowId" => "row_1",
                        "first_name"=> "Tiger",
                        "last_name"=>  "Nixon",
                        "position"=>  "System Architect",
                        "email"=>  "[email protected]",
                        "office"=>  "Edinburgh",
                        "extn" =>  5421,
                        "age"=>  61,
                        "salary"=>  20800,
                        "start_date"=>  "2011-04-25"
            ),
));
echo json_encode($json, JSON_FORCE_OBJECT);

and the output is:

{
    "Data": {
        "1": {
            "DT_RowId": "row_1",
            "first_name": "Tiger",
            "last_name": "Nixon",
            "position": "System Architect",
            "email": "[email protected]",
            "office": "Edinburgh",
            "extn": 5421,
            "age": 61,
            "salary": 20800,
            "start_date": "2011-04-25"
        },
        "2": {
            "DT_RowId": "row_1",
            "first_name": "Tiger",
            "last_name": "Nixon",
            "position": "System Architect",
            "email": "[email protected]",
            "office": "Edinburgh",
            "extn": 5421,
            "age": 61,
            "salary": 20800,
            "start_date": "2011-04-25"
        }
    }
}
4
  • How about not using an associative array in the PHP to begin with? Commented Apr 18, 2021 at 21:39
  • well i need to create that array from a foreach. You have sugestion how to do it? Commented Apr 18, 2021 at 21:41
  • A foreach of what? We have no idea what the original source data is Commented Apr 18, 2021 at 21:43
  • data is coming from server. Need to run a foreach from the result of the query and creat that json or creat an array and then encode it. Commented Apr 18, 2021 at 21:45

1 Answer 1

1

So i messed around and i finally got it to work.

$mysqli = new mysqli("127.0.0.1", "root", "", "datatables_demo");

$q = 'SELECT * FROM datatables_demo';
$result = $mysqli->query($q);
while($row = $result->fetch_assoc())
{
    $resultarray[] = $row;
}

$data = array();
    foreach ($resultarray as $info)
    {
        array_push($data, $info);
    }

$json = array(
    'data' => $data
);

echo json_encode($json);

And teh result output as expected:

{
    "data": [{
        "id": "1",
        "first_name": "jhon",
        "last_name": "grcfsdia",
        "position": "ceo",
        "email": "[email protected]",
        "office": "aenbx",
        "extn": "23",
        "age": "422",
        "salary": "19999.00",
        "start_date": "05-01-1490"
    }, {
        "id": "2",
        "first_name": "srte",
        "last_name": "gabdr",
        "position": "ceASFo",
        "email": "[email protected]",
        "office": "aawwwa",
        "extn": "2343",
        "age": "422",
        "salary": "19999.00",
        "start_date": "15-11-1911"
    }]
}
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.