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. 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.

$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.

Read More
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. 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"}
}]

Related posts

Leave a Reply

1 comment

  1. 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.

    1. build your own array
    2. iterate over your sql result set while adding your own markup
    3. send the output to 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