Reputation: 23
i have generated a json according to my sql query out put here is my json output, i have listed few of my json code
"42":"",
"field_id_25":"",
"43":null,
"field_ft_25":null,
"44":"",
"field_id_26":"",
"45":null,
"field_ft_26":null,
"46":"",
"field_id_27":"",
"47":null,
"field_ft_27":null,
i have to change json key field_ft_$$ according to my database table values..
|---------------------|------------------|
| field id | field name |
|---------------------|------------------|
| field_id_27 | meta_description |
|---------------------|------------------|
ive coded in php then after i dont know how to finish..
<?php
$con=mysqli_connect("localhost","root","","penn");
if (!$con) {
echo "Failed";
}
// Retrive Data From WebLog Title
$sql="SELECT * FROM `exp_weblog_titles` LEFT JOIN `exp_weblog_data` ON `exp_weblog_data`.`entry_id` = `exp_weblog_titles`.`entry_id` WHERE exp_weblog_titles.weblog_id='13' LIMIT 2 ";
$query=mysqli_query($con,$sql);
while ($row=mysqli_fetch_array($query))
{
$data[] = $row;
}
// Convert The Weblog title data to json
$json_string = json_encode($data, JSON_PRETTY_PRINT);
echo $json_string;
// Retrive web_log_field_List as a array from database
$sql1="SELECT field_id,field_name FROM exp_weblog_fields";
$query1=mysqli_query($con,$sql1);
while ($row1=mysqli_fetch_array($query1)) {
$web_log_field[$row1['field_id']] = $row1['field_name'];
}
// echo $web_log_field[21];
?>
example - i need to change "field_ft_27":null to "meta_description": null and others according to my database values
Upvotes: 0
Views: 57
Reputation: 781848
Use a regular expression to extract the appropriate parts of field_ft_XX
and get the corresponding field_id_XX
element of $web_log_field
. Then you can delete the old key and add the replacement key.
foreach ($data as &$row) {
foreach ($row as $key => $value) {
if (preg_match('/^field_ft_(\d+)$/', $key)) {
$idkey = "field_id_$key[1]";
if (isset($web_log_field[$idkey])) {
$row[$web_log_field[$idkey]] = $value;
unset($row[$key]);
}
}
}
}
Upvotes: 1