dsgrs26202 2014-09-28 18:13
浏览 30
已采纳

数组到javascript字符串

So I have this string:

[{values: {"US" : "rgba(29, 79, 207, 1)"}},{values: {"NL" : "rgba(29, 79, 207, 1)"}}] This string sets colours for jvectormap. the entire reason for this function, I will only show countries that visited.

It's from Javascript, and I need to use this same format but in PHP and I think it has something to do with json encode but I can't get it to work.. So what I want to do is make an array into this string.. How would I do that?

When I make an array in PHP and encode it to json, this comes out: [["NL","black"],["US","blue"]] and this format wont work of course.

  • 写回答

1条回答 默认 最新

  • donglianglu8136 2014-09-28 18:17
    关注

    You've supplied invalid JSON. values needs to be quoted to become "values", for PHP to decode it.

    <?php
    
    $string = <<<JSON
    [{"values": {"US" : "rgba(29, 79, 207, 1)"}},{"values": {"NL" : "rgba(29, 79, 207, 1)"}}]
    JSON;
    
    echo print_r( json_decode($string, true), true);
    

    https://eval.in/199639

    An excellent JSON checker is http://json.parser.online.fr

    You'd then create your array like;

    $array = array (
      0 => 
      array (
        'values' => 
        array (
          'US' => 'rgba(29, 79, 207, 1)',
        ),
      ),
      1 => 
      array (
        'values' => 
        array (
          'NL' => 'rgba(29, 79, 207, 1)',
        ),
      ),
    );
    echo json_encode($array);
    

    https://eval.in/199684

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录