John Davidson

php - Costume data set datatable

0 comments
Message:


I have json from url like this


{
"data":[
{
"id":"01",
"name":"Some.one"
},
{
"id":"02",
"name":"Some.two"
}
]
}

How to add costume data to show at datatabale ?


| id | name      |     web       |
| 01 | Some.one | Someone.com |
| 02 | Some.two | Sometwo.com |

In this case I want to add one more table where the contents are the data from the name column plus ".com"



My Answer:
"costumes": [
{
"costume_id": 1,
"costume_name": "Pirate",
"costume_type": "Adult",
"costume_price": 25.99
},
{
"costume_id": 2,
"costume_name": "Princess",
"costume_type": "Child",
"costume_price": 19.99
},
{
"costume_id": 3,
"costume_name": "Superhero",
"costume_type": "Adult",
"costume_price": 29.99
}
]
}

You can create a datatable in PHP to store this costume data set by decoding the JSON and looping through the costumes array to insert each costume into the datatable. Here's an example code snippet:

php
$json_data = '{
"costumes": [
{
"costume_id": 1,
"costume_name": "Pirate",
"costume_type": "Adult",
"costume_price": 25.99
},
{
"costume_id": 2,
"costume_name": "Princess",
"costume_type": "Child",
"costume_price": 19.99
},
{
"costume_id": 3,
"costume_name": "Superhero",
"costume_type": "Adult",
"costume_price": 29.99
}
]
}';

$data = json_decode($json_data, true);

$datatable = [];

foreach ($data['costumes'] as $costume) {
$datatable[] = [
'costume_id' => $costume['costume_id'],
'costume_name' => $costume['costume_name'],
'costume_type' => $costume['costume_type'],
'costume_price' => $costume['costume_price']
];
}

// Now $datatable contains the costume data set in a datatable format
print_r($datatable);


This code snippet will decode the JSON data, loop through the costumes array, and insert each costume into the $datatable array. You can then use this $datatable array to perform operations on the costume data set in PHP.

Rate this post

3 of 5 based on 1441 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us