1

I use the following PHP lines to convert some simpleXML data to an array:

$dataRaw = array();
foreach($objCount->escalations as $esc) {
    $dataRaw[(string)$esc->region] = (int)$esc->volume;
}
$dataPrep = json_decode(json_encode($dataRaw), TRUE);

Printing this returns the following which looks ok to me:

Array ( [af] => 6 [as] => 295 [eu] => 249 [na] => 279 [oc] => 42 [sa] => 10 ) 

I then tried to pass this in a JS function using the following line but this doesn't work. I am not getting any errors, the chart just doesnt show at all with this, probably because it doesn't recognise the content.

data: <?php echo $dataPrep;?>

When I hard-code the "data" values in JS as follows then everything works fine so I am probably passing it wrong.

var data = [{ 'hc-key': 'af', value: 6 },
    { 'hc-key': 'as', value: 295 },
    { 'hc-key': 'eu', value: 249 },
    { 'hc-key': 'na', value: 279 },
    { 'hc-key': 'oc', value: 42 },
    { 'hc-key': 'sa', value: 10 }];

Can some tell me what I have to change here ?

Many thanks in advance, Tim.

user2571510
  • 11,167
  • 39
  • 92
  • 138

4 Answers4

4

You need to pass the encoded data, not after decoding it. As after decoding, it change into array and directly we can not assign array to js variable:

foreach($objCount->escalations as $esc) {
    $dataRaw[(string)$esc->region] = (int)$esc->volume;
}
$dataPrep = json_encode($dataRaw);

and for js variable :

data: <?php echo $dataPrep;?>
prava
  • 3,916
  • 2
  • 24
  • 35
  • Thanks a lot for the quick reply ! This works great - will accept as soon as I can. :) – user2571510 Jun 23 '14 at 13:15
  • It turned out that the below response from Danijel worked better in my case (probably because of the hc-key part) so I'll go with that one. Thanks anyways for bringing me on the right track ! – user2571510 Jun 23 '14 at 13:21
3
data: <?php echo json_encode($dataPrep); ?>

PHP manual: json_encode()http://www.php.net/manual/en/function.json-encode.php

Coby
  • 1,528
  • 9
  • 8
2

Try this to get the desired structure:

foreach($objCount->escalations as $esc) {
    $dataRaw[] = array( 'hc-key' => (string) $esc->region, 'value' => (int) $esc->volume );
}

data: <?php echo json_encode( $dataRaw ); ?>
Danijel
  • 12,408
  • 5
  • 38
  • 54
  • Thanks a lot for this ! It turned out that this worked better in my case (probably because of the hc-key part) so I'll go with that one. Will accept as soon as I can. – user2571510 Jun 23 '14 at 13:22
1

When you json_encode your data, you will have to parse it as json in javascript before you have an array again. Use for example: http://api.jquery.com/jquery.parsejson/

But in your case, you have already converted it back to a php array by calling json_decode. You cannot echo a php-array and expect it to show in the source like a javascript array.

marty
  • 651
  • 7
  • 13
  • No you can echo it as the other answers have done, no need to use javascript's JSON.parse in this case. – James Jun 23 '14 at 13:20
  • @James, i was refering to this line: `$dataPrep = json_decode(json_encode($dataRaw), TRUE);` Here, $dataPrep will be a php-array again. `echo $dataPrep` will result in simply `Array` as output – marty Jun 23 '14 at 13:25