简体   繁体   English

从Wordpress $ wpdb-> get_results数组构建新的自定义数组

[英]Build new custom Array from Wordpress $wpdb->get_results array

I'm currently taking the results of a table and using wp_send_json to using it as a JSON response. 我目前正在获取表的结果,并使用wp_send_json将其用作JSON响应。 The data is encoded as expected, however I'd like to tweak the output a bit by changing the keys, formating, and order. 数据按预期进行了编码,但是我想通过更改键,格式和顺序来稍微调整输出。 I'm not sure how to rebuild the array and encode as json after so I'm looking for a little bit of help. 我不确定如何重建数组并在之后编码为json,所以我在寻求帮助。

$stuff= $wpdb->get_results( $wpdb->prepare("SELECT * FROM wp_table"), ARRAY_A);
wp_send_json($stuff);

As of now the results I get via print_r look as follows. 到目前为止,我通过print_r获得的结果如下所示。

Array(
    [0] => Array(
        [id] => 1[gender] => Male[email] => test@loas . com[lat] => 38[long] => - 97[country_srt] => USA[country_long] => UnitedStates
    ) [1] => Array(
        [id] => 2[gender] => Female[email] => femal@test . com[lat] => 38[long] => - 97[country_srt] => USA[country_long] => UnitedStates
    )
)

When encoded I get: 编码后,我得到:

[{
    "id": "1",
    "gender": "Male",
    "email": "test@loas.com",
    "lat": "45",
    "long": "-76",
    "country_srt": "USA",
    "country_long": "United States"
}, {
    "id": "2",
    "gender": "Female",
    "email": "femal@test.com",
    "lat": "98",
    "long": "-34",
    "country_srt": "USA",
    "country_long": "United States"
}]

Thing is, I don't really need some of these values and also need to format some things to output for easy map plotting. 事实是,我真的不需要这些值中的任何一个,也不需要格式化一些东西以输出即可进行简单的地图绘制。 For instance the country longform and gender go into an html formatted string. 例如,国家长格式和性别输入html格式的字符串。 What I'm looking to do is transform this array to result in: 我想要做的是将这个数组转换为结果:

[ idhere: {
    "value": "1",
    "latitude": "45",
    "longitude": "-76",
    "tooltip": {"content":"HTML Showing gender variable and country variable"}
}, idhere: {
    "value": "2",
    "latitude": "98",
    "longitude": "-34",
    "tooltip": {"content":"HTML Showing gender variable and country variable"}
}]

I think what you need to do is break down the process down into steps (so you can change the data around) instead of sending your sql data to json directly. 我认为您需要做的是将流程分解为多个步骤(以便可以更改数据),而不是将sql数据直接发送到json。

  1. build your own array 建立自己的数组
  2. iterate over your sql result set while adding your own markup 在添加自己的标记时遍历sql结果集
  3. send the output to json 将输出发送到json

something like: 就像是:

$preJSON = array();    

// select only columns you need
$sql = "SELECT id, gender, country_srt, lat, long
        FROM wp_table"


$count = 0; // this is for $preJSON[] index

foreach( $wpdb->get_results( $sql ) as $key => $row ) {

    // each column in your row will now be accessible like this: 
    // $my_column = $row->column_name;
    // now we can do:

    $value = $row->id;
    $latitude = $row->lat;
    $longitude = $row->long;
    $gender = $row->gender;
    $country = $row->country_srt;
    $tooltip = array(
        "content" => "HTML and stuff" . $gender . "more HTML and stuff" . $country
    );

    // now we can build a row of this information in our master array
    $preJSON[$count] = array(
        "value" => $value,
        "latitude" => $latitude,
        "longitude" => $longitude,
        "tooltip" => $tooltip
    );

    // increment the index
    ++$count;
}

// after foreach
// send the whole array to json
$json = json_encode( $preJSON );

I believe this should be the basic gist of what you need 我相信这应该是您所需要的基本要点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM