I just get started Wordpress and trying to change a query result to json in PHP and following is the query result.
user_id meta_key meta_value
1 nickname bko117
1 first_name seokho
1 last_name baek
1 genba Company
1 Department 00
2 nickname themedemos
2 first_name
2 last_name
3 nickname john
.....
I want to change this data to json data
e.g :
"data":[
{"nickname":"bko117", "first_name":"seokho","lastname":"baek","genba":"company","Department":"00" },
{"nickname":"themedemos", "first_name":"","lastname":"","genba":"","Department":"" },
{"nickname":"john", "first_name":"","lastname":"","genba":"","Department":"" },
.......
]
When user_id changed, create new json array and put next on it.
This is what I want to make, and I've tried for more than 2 hours but still struggling with this matter.
Next code is what I've done so far.
<?php
echo "1<br>";
global $wpdb;
$results = $wpdb->get_results("SELECT `user_id` , `meta_key` , `meta_value`
FROM `wp_usermeta`
WHERE `meta_key`
IN (
'nickname', 'first_name', 'last_name', 'Department', 'genba'
)
ORDER BY `user_id` ASC
LIMIT 0 , 999999");
echo "2<br>";
//print_r($results);
//print json_encode($results);
$dataArray = array();
echo "3<br>";
echo "4<br>";
foreach($results as $row){
echo $row -> meta_key, ':';
echo $row -> meta_value, '<br>';
}
print_r($data);
?>
Thank you in advance for your attention to this matter.