0

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.

2 Answers 2

1

Use as below :

$dataArray = array();
foreach($results as $row){
    $dataArray[$row->user_id][$row->meta_key] = $row->meta_value;
}
print_r(json_encode($dataArray));
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you. now I got [{"nickname":"bko117"},{"first_name":"seokho"},{"last_name":"baek"},{"genba":"Company"},{"Department":"00"},{"nickname":"themedemos"}... This is better answer but I still want to get like this -> {"nickname":"bko117", "first_name":"seokho","lastname":"baek","genba":"company","Department":"00" }
Will you add print_r($results); output here?
Here is print_r($results); output. Array ( [0] => stdClass Object ( [user_id] => 1 [meta_key] => nickname [meta_value] => bko117 ) [1] => stdClass Object ( [user_id] => 1 [meta_key] => first_name [meta_value] => seokho ) [2] => stdClass Object ( [user_id] => 1 [meta_key] => last_name [meta_value] => baek ) [3] => stdClass Object ( [user_id] => 1 [meta_key] => genba [meta_value] => Company ) [4] => stdClass Object ( [user_id] => 1 [meta_key] => Department [meta_value] => 00 ) [5] => stdClass Object ( [user_id] => 2 [meta_key] => nickname [meta_value] => themedemos )
Thanks a lot! finally it works. Now I got {"1":{"nickname":"bko117","first_name":"seokho","last_name":"baek","genba":"Company","Department":"00"},"2":{"nickname":"themedemos","first_name":"","last_name":""},
0

You could use json_encode on the array with the desired structure, like so:

$dataArray = array();
foreach($results as $row){
$dataArray[$row->meta_key] = $row->meta_value;
}

print_r(json_encode($dataArray));

1 Comment

thank you for replying. I tried this code and it works somehow but I only get a last one. just {"nickname":"james","first_name":"\u672c\uff2a","last_name":"\u30e4\u30f3","genba":"Company","Department":"3"}, how could i get all data?

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.