I just added a custom Highchart to my wordpress single custom archive page. http://propertywise.malisa.agency/en/district/khlong-toei-2/

Using the following function call from within the Highcharts jQuery(document).ready(function():

 series: [{
        type: 'column',
        name : '<?php echo get_the_title();?>',
        data: <?php echo average_bedroom_rental_price_for_district($post->ID, '34','0', 'district', $type="DESC" );?>

This works fine and generates the chart as expected, except for one thing. The expected data array needs to have a comma ending the array, example:


Unfortunately, my output (although works) is thus:


without the trailing comma and this breaks all jQuery(Google Map Marker) on the page after showing the chart.

I have read, studied and tried almost everything I can think of to generate the array in a different manner hoping to get the output as needed, but to no avail, so, I'm really hoping someone can advise and either correct my mistake or advise the best way to create the data from php to jQuery.

if it helps in someone helping with an answer, this is my php function :

function average_bedroom_rental_price_for_district($district_id,     $contract_type='34',$beds='0', $avg_for, $type="DESC" ){

$numbers_array = array(0,1,2,3,4,5);
$current_array = array();
$average_rental_price_distict_array = array();
$post_id = $id_post;

foreach ($numbers_array as $key)
  $avg_rental_price_args = array(
        'post_type' => 'dt_properties',
        'posts_per_page' => -1,
        'tax_query' => array(
                'taxonomy' => 'contract_type',
                'field' => 'id',
                'terms' => $contract_type
        'meta_query' => array(
            'relation' => 'AND',
        'value'=> $key,
        'compare' => '=',
        'type'    => 'numeric',
        'value'=> $district_id,
                'compare' => 'LIKE',

$avg_rental_price_query = new WP_Query($avg_rental_price_args);
    $total_average_price = 0;
if( $avg_rental_price_query->have_posts() ):
          $totalprice = 0;
          $pre_price = 0;

while ( $avg_rental_price_query->have_posts() ):

    $the_id = get_the_ID();
    $pre_price = get_post_meta ( $the_id, "_property_price",true);
    $number_bedrooms = $key;
    $totalprice += $pre_price;

            $total_average_price =  ($totalprice/$i);

            $current_array[] = array($key,round_up($total_average_price));


$averagePricejson = json_encode($current_array);

return $averagePricejson;

1 Answer 1


If it's just a simple tweak to the output string then just use str_replace.

$averagePricejson = "[[0,0],[1,39000],[2,62500],[3,80000]]";

$averagePricejson = str_replace( "]]", "],]", $averagePricejson);

echo $averagePricejson; // [[0,0],[1,39000],[2,62500],[3,80000],]
  • Hey, Thank you very much, this is indeed the correct answer and I appreciate your quick response. Looks like I'm doing something else wrong here, maybe the way I'm genrating the array. After changing the code to your answer, I get the correct output via the json_encode: [[0,18000],[1,47500],[2,87500],[3,0],[4,0],[5,0],] But it still breaks the map marker, by psting this in to the Jquery function direct, it works all ok.. Hmmm.. another headache
    – Malisa
    Dec 14, 2015 at 15:30
  • Try wrapping the value with " for data: "<?php echo 1234; ?>".
    – jgraup
    Dec 14, 2015 at 15:38
  • Thanks jgraup, but this doesnt work either, seems I can get now, the correct output when viewing source: [[0,18000],[1,47500],[2,87500],[3,0],[4,0],[5,0],] but it still breaks the google map markers when using the php function. If I paste the output direct into the series data, its fine, so not sure where to go
    – Malisa
    Dec 14, 2015 at 16:08
  • What about if you output the json string, wrapped with quotes, and pass it through the JSON decoder before setting the data? data: JSON.parse( "<?php echo 1234; ?>" )
    – jgraup
    Dec 14, 2015 at 16:15
  • jgraup, I just want to thank you again for taking the time to help me with this, its much appreciated. I solved the problem with your help.
    – Malisa
    Dec 14, 2015 at 17:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.