【问题标题】:Build new custom Array from Wordpress $wpdb->get_results array从 Wordpress $wpdb->get_results 数组构建新的自定义数组
【发布时间】:2015-03-09 03:41:44
【问题描述】:

我目前正在获取表格的结果并使用 wp_send_json 将其用作 JSON 响应。数据按预期编码,但是我想通过更改键、格式和顺序来稍微调整输出。我不确定如何重建数组并编码为 json,所以我正在寻找一些帮助。

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

到目前为止,我通过 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
    )
)

编码后我得到:

[{
    "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"
}]

问题是,我真的不需要其中的一些值,并且还需要格式化一些内容以输出以便于绘制地图。例如,国家长格式和性别进入一个 html 格式的字符串。我要做的是转换这个数组以产生:

[ 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"}
}]

【问题讨论】:

  • 然后只需更改您的查询,只需包含您想要的列
  • 改变我的查询方式?我不确定我是否遵循这个简单的答案。
  • @Ghost 将查询限制在所需的特定列并不能解决将您自己的标记添加到最终 json 的方面
  • @DrewT 当然还有更多内容,这就是为什么它是一个评论 :),而 $count 是多余的,只需像往常一样推送它 $array[] = array here
  • @Ghost 运行增量 $count 索引在技术上比在 php 中推送到数组在性能上更快。请参阅此线程中的基准:stackoverflow.com/questions/559844/… 这也是一种首选方法,因为它有助于调试循环,因为您可以在任何断点处输出它的值。

标签: php arrays json wordpress


【解决方案1】:

我认为您需要做的是将流程分解为步骤(以便您可以更改数据),而不是直接将您的 sql 数据发送到 json。

  1. 构建自己的数组
  2. 在添加您自己的标记时迭代您的 sql 结果集
  3. 将输出发送到 json

类似:

$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 );

我相信这应该是您需要的基本要点

【讨论】:

  • 这很有意义,但由于某种原因,数组变为空。
  • 您可能没有得到正确的$key => $row 对。如果您尝试foreach( $wpdb->get_results( $sql ) as $row ) { print_r( $row ) } 会发生什么,如果查询不为空,它应该会显示密钥对
  • 问题似乎是 SQL 中的 long 列。经度包含负数,当我尝试只输出一列时,我得到一个空数组。是否有某些原因负数不会打印出来并清空数组?这是我在所有列中看到的唯一区别。
  • @Mr.BigglesWorth 我真的需要知道你打印出整个 $row 会发生什么,就像我上面建议的那样。否则,我在猜测我何时提供建议。我的猜测是听起来你没有以正确的类型获取信息并且它打破了循环。您可以尝试对行进行字符串转换,例如:$longitude = (string) $row->long;
  • 顺便问一下,你是说只要你不要求经度行,我的答案就有效吗?
猜你喜欢
  • 2014-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-17
  • 2016-02-13
  • 1970-01-01
相关资源
最近更新 更多