当前位置: 首页 > 面试题库 >

JSON PHP数组到Javascript

莘光华
2023-03-14
问题内容

是否可以通过json_encode对这个php数组进行JSON转换?因为这个php数组被调用$data,当我做..

var myJson = <?php echo json_encode($data) ?>;
console.log(myJson);

在JavaScript中,它给了我错误。我想知道JSON可以/不能做什么类型的php数组是否有限制。

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [text] => SimpleXMLElement Object
                        (
                            [0] => Tanner, C. kickoff 60 yards to the DU5, Butler, L return 14 yards to the DU19 (Johnson, I.).
                        )

                    [spot] => SimpleXMLElement Object
                        (
                            [0] => GT35
                        )

                    [ball] => SimpleXMLElement Object
                        (
                            [0] => GT
                        )

                    [togo] => SimpleXMLElement Object
                        (
                            [0] => 10
                        )

                    [type] => SimpleXMLElement Object
                        (
                            [0] => K
                        )

                    [drive] => 1
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [text] => SimpleXMLElement Object
                        (
                            [0] => Renfree, S pass complete to Vernon, C for 6 yards to the DU25 (Thomas, Je.).
                        )

                    [spot] => SimpleXMLElement Object
                        (
                            [0] => DU19
                        )

                    [ball] => SimpleXMLElement Object
                        (
                            [0] => DU
                        )

                    [togo] => SimpleXMLElement Object
                        (
                            [0] => 10
                        )

                    [type] => SimpleXMLElement Object
                        (
                            [0] => P
                        )

                    [drive] => 2
                )

            [1] => Array
                (
                    [text] => SimpleXMLElement Object
                        (
                            [0] => Renfree, S pass incomplete to Scott, D.
                        )

                    [spot] => SimpleXMLElement Object
                        (
                            [0] => DU25
                        )

                    [ball] => SimpleXMLElement Object
                        (
                            [0] => DU
                        )

                    [togo] => SimpleXMLElement Object
                        (
                            [0] => 4
                        )

                    [type] => SimpleXMLElement Object
                        (
                            [0] => P
                        )

                    [drive] => 2
                )

            [2] => Array
                (
                    [text] => SimpleXMLElement Object
                        (
                            [0] => Renfree, S pass complete to Scott, D for 6 yards to the DU31, 1ST DOWN DU (Thomas, Je.).
                        )

                    [spot] => SimpleXMLElement Object
                        (
                            [0] => DU25
                        )

                    [ball] => SimpleXMLElement Object
                        (
                            [0] => DU
                        )

                    [togo] => SimpleXMLElement Object
                        (
                            [0] => 4
                        )

                    [type] => SimpleXMLElement Object
                        (
                            [0] => P
                        )

                    [drive] => 2
                )

            [3] => Array
                (
                    [text] => SimpleXMLElement Object
                        (
                            [0] => Renfree, S pass complete to Scott, D for no gain to the DU31 (Sweeting, R.).
                        )

                    [spot] => SimpleXMLElement Object
                        (
                            [0] => DU31
                        )

                    [ball] => SimpleXMLElement Object
                        (
                            [0] => DU
                        )

                    [togo] => SimpleXMLElement Object
                        (
                            [0] => 10
                        )

                    [type] => SimpleXMLElement Object
                        (
                            [0] => P
                        )

                    [drive] => 2
                )

            [4] => Array
                (
                    [text] => SimpleXMLElement Object
                        (
                            [0] => Renfree, S pass incomplete to Vernon, C.
                        )

                    [spot] => SimpleXMLElement Object
                        (
                            [0] => DU31
                        )

                    [ball] => SimpleXMLElement Object
                        (
                            [0] => DU
                        )

                    [togo] => SimpleXMLElement Object
                        (
                            [0] => 10
                        )

                    [type] => SimpleXMLElement Object
                        (
                            [0] => P
                        )

                    [drive] => 2
                )

            [5] => Array
                (
                    [text] => SimpleXMLElement Object
                        (
                            [0] => Renfree, S sacked for loss of 4 yards to the DU27 (Gotsis, A.).
                        )

                    [spot] => SimpleXMLElement Object
                        (
                            [0] => DU31
                        )

                    [ball] => SimpleXMLElement Object
                        (
                            [0] => DU
                        )

                    [togo] => SimpleXMLElement Object
                        (
                            [0] => 10
                        )

                    [type] => SimpleXMLElement Object
                        (
                            [0] => P
                        )

                    [drive] => 2
                )

            [6] => Array
                (
                    [text] => SimpleXMLElement Object
                        (
                            [0] => Monday, W punt 53 yards to the GT20, Golden, J. return 8 yards to the GT28 (Reeves, D).
                        )

                    [spot] => SimpleXMLElement Object
                        (
                            [0] => DU27
                        )

                    [ball] => SimpleXMLElement Object
                        (
                            [0] => DU
                        )

                    [togo] => SimpleXMLElement Object
                        (
                            [0] => 14
                        )

                    [type] => SimpleXMLElement Object
                        (
                            [0] => U
                        )

                    [drive] => 2
                )

        )

    [2] => Array
        (
            [0] => Array
                (
                    [text] => SimpleXMLElement Object
                        (
                            [0] => Smith, O. rush for 8 yards to the GT36 (Brown, Ky).
                        )

                    [spot] => SimpleXMLElement Object
                        (
                            [0] => GT28
                        )

                    [ball] => SimpleXMLElement Object
                        (
                            [0] => GT
                        )

                    [togo] => SimpleXMLElement Object
                        (
                            [0] => 10
                        )

                    [type] => SimpleXMLElement Object
                        (
                            [0] => R
                        )

                    [drive] => 3
                )

            [1] => Array
                (
                    [text] => SimpleXMLElement Object
                        (
                            [0] => Sims, D. rush for 6 yards to the GT42, 1ST DOWN GT (Sink, N).
                        )

                    [spot] => SimpleXMLElement Object
                        (
                            [0] => GT36
                        )

                    [ball] => SimpleXMLElement Object
                        (
                            [0] => GT
                        )

                    [togo] => SimpleXMLElement Object
                        (
                            [0] => 2
                        )

                    [type] => SimpleXMLElement Object
                        (
                            [0] => R
                        )

                    [drive] => 3
                )

            [2] => Array
                (
                    [text] => SimpleXMLElement Object
                        (
                            [0] => Washington, T. pass incomplete to Smith, O..
                        )

                    [spot] => SimpleXMLElement Object
                        (
                            [0] => GT42
                        )

                    [ball] => SimpleXMLElement Object
                        (
                            [0] => GT
                        )

                    [togo] => SimpleXMLElement Object
                        (
                            [0] => 10
                        )

                    [type] => SimpleXMLElement Object
                        (
                            [0] => P
                        )

                    [drive] => 3
                )

            [3] => Array
                (
                    [text] => SimpleXMLElement Object
                        (
                            [0] => Washington, T. rush for 2 yards to the GT44 (Anunike, K).
                        )

                    [spot] => SimpleXMLElement Object
                        (
                            [0] => GT42
                        )

                    [ball] => SimpleXMLElement Object
                        (
                            [0] => GT
                        )

                    [togo] => SimpleXMLElement Object
                        (
                            [0] => 10
                        )

                    [type] => SimpleXMLElement Object
                        (
                            [0] => R
                        )

                    [drive] => 3
                )

            [4] => Array
                (
                    [text] => SimpleXMLElement Object
                        (
                            [0] => Greene, J. rush for 6 yards to the 50 yardline (Canty, W).
                        )

                    [spot] => SimpleXMLElement Object
                        (
                            [0] => GT44
                        )

                    [ball] => SimpleXMLElement Object
                        (
                            [0] => GT
                        )

                    [togo] => SimpleXMLElement Object
                        (
                            [0] => 8
                        )

                    [type] => SimpleXMLElement Object
                        (
                            [0] => R
                        )

                    [drive] => 3
                )

            [5] => Array
                (
                    [text] => SimpleXMLElement Object
                        (
                            [0] => Smith, O. rush for 16 yards to the DU34, 1ST DOWN GT (Canty, W;France, C).
                        )

                    [spot] => SimpleXMLElement Object
                        (
                            [0] => GT50
                        )

                    [ball] => SimpleXMLElement Object
                        (
                            [0] => GT
                        )

                    [togo] => SimpleXMLElement Object
                        (
                            [0] => 2
                        )

                    [type] => SimpleXMLElement Object
                        (
                            [0] => R
                        )

                    [drive] => 3
                )

            [6] => Array
                (
                    [text] => SimpleXMLElement Object
                        (
                            [0] => Smith, O. rush for 5 yards to the DU29 (France, C).
                        )

                    [spot] => SimpleXMLElement Object
                        (
                            [0] => DU34
                        )

                    [ball] => SimpleXMLElement Object
                        (
                            [0] => GT
                        )

                    [togo] => SimpleXMLElement Object
                        (
                            [0] => 10
                        )

                    [type] => SimpleXMLElement Object
                        (
                            [0] => R
                        )

                    [drive] => 3
                )

            [7] => Array
                (
                    [text] => SimpleXMLElement Object
                        (
                            [0] => Laskey, Z. rush for 3 yards to the DU26 (France, C).
                        )

                    [spot] => SimpleXMLElement Object
                        (
                            [0] => DU29
                        )

                    [ball] => SimpleXMLElement Object
                        (
                            [0] => GT
                        )

                    [togo] => SimpleXMLElement Object
                        (
                            [0] => 5
                        )

                    [type] => SimpleXMLElement Object
                        (
                            [0] => R
                        )

                    [drive] => 3
                )

            [8] => Array
                (
                    [text] => SimpleXMLElement Object
                        (
                            [0] => Washington, T. rush for 3 yards to the DU23, 1ST DOWN GT (France, C).
                        )

                    [spot] => SimpleXMLElement Object
                        (
                            [0] => DU26
                        )

                    [ball] => SimpleXMLElement Object
                        (
                            [0] => GT
                        )

                    [togo] => SimpleXMLElement Object
                        (
                            [0] => 2
                        )

                    [type] => SimpleXMLElement Object
                        (
                            [0] => R
                        )

                    [drive] => 3
                )

            [9] => Array
                (
                    [text] => SimpleXMLElement Object
                        (
                            [0] => Washington, T. pass complete to Smith, O. for 21 yards to the DU2, 1ST DOWN GT (Butler, L).
                        )

                    [spot] => SimpleXMLElement Object
                        (
                            [0] => DU23
                        )

                    [ball] => SimpleXMLElement Object
                        (
                            [0] => GT
                        )

                    [togo] => SimpleXMLElement Object
                        (
                            [0] => 10
                        )

                    [type] => SimpleXMLElement Object
                        (
                            [0] => P
                        )

                    [drive] => 3
                )

            [10] => Array
                (
                    [text] => SimpleXMLElement Object
                        (
                            [0] => Washington, T. rush for 1 yard to the DU1 (Brown, Ky;Foxx, J).
                        )

                    [spot] => SimpleXMLElement Object
                        (
                            [0] => DU02
                        )

                    [ball] => SimpleXMLElement Object
                        (
                            [0] => GT
                        )

                    [togo] => SimpleXMLElement Object
                        (
                            [0] => 0
                        )

                    [type] => SimpleXMLElement Object
                        (
                            [0] => R
                        )

                    [drive] => 3
                )

            [11] => Array
                (
                    [text] => SimpleXMLElement Object
                        (
                            [0] => Washington, T. rush for no gain to the DU1 (Cockrell, R).
                        )

                    [spot] => SimpleXMLElement Object
                        (
                            [0] => DU01
                        )

                    [ball] => SimpleXMLElement Object
                        (
                            [0] => GT
                        )

                    [togo] => SimpleXMLElement Object
                        (
                            [0] => 0
                        )

                    [type] => SimpleXMLElement Object
                        (
                            [0] => R
                        )

                    [drive] => 3
                )

            [12] => Array
                (
                    [text] => SimpleXMLElement Object
                        (
                            [0] => Washington, T. rush for 1 yard to the DU0, TOUCHDOWN, clock 06:51.
                        )

                    [spot] => SimpleXMLElement Object
                        (
                            [0] => DU01
                        )

                    [ball] => SimpleXMLElement Object
                        (
                            [0] => GT
                        )

                    [togo] => SimpleXMLElement Object
                        (
                            [0] => 0
                        )

                    [type] => SimpleXMLElement Object
                        (
                            [0] => R
                        )

                    [drive] => 3
                )

            [13] => Array
                (
                    [text] => SimpleXMLElement Object
                        (
                            [0] => Tanner, C. kick attempt good.
                        )

                    [spot] => SimpleXMLElement Object
                        (
                            [0] => DU03
                        )

                    [ball] => SimpleXMLElement Object
                        (
                            [0] => GT
                        )

                    [togo] => SimpleXMLElement Object
                        (
                            [0] => 0
                        )

                    [type] => SimpleXMLElement Object
                        (
                            [0] => X
                        )

                    [drive] => 3
                )

            [14] => Array
                (
                    [text] => SimpleXMLElement Object
                        (
                            [0] => Tanner, C. kickoff 54 yards to the DU11, Butler, L return 23 yards to the DU34 (Harrell, A.).
                        )

                    [spot] => SimpleXMLElement Object
                        (
                            [0] => GT35
                        )

                    [ball] => SimpleXMLElement Object
                        (
                            [0] => GT
                        )

                    [togo] => SimpleXMLElement Object
                        (
                            [0] => 0
                        )

                    [type] => SimpleXMLElement Object
                        (
                            [0] => K
                        )

                    [drive] => 3
                )

        )
)

问题答案:

首先,您需要在安装了PHP的Apache服务器上的一个PHP文件。制作如下文件:

localhost:8888/myfile.php

<?php
    $arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);

    echo json_encode($arr); // {"a":1,"b":2,"c":3,"d":4,"e":5}
?>

然后是您的JavaScript(在此示例中,我使用jQuery):

$.getJSON('http://localhost:8888/myfile.php', function(data) {
    console.log(data);
});

这应该是开始将PHP数组添加到JavaScript中的开始。



 类似资料:
  • 我正在尝试用java swing制作一个Tic-Tac-Toe程序,我已经制作了框架。如何让JButton数组中的按钮激活int数组?我希望int数组保存Tic-Tac-Toe网格中点的值,因此当按下按钮时,int数组中的对应点将是0或1,按钮的文本将更改为X或O。

  • 问题内容: 我在处理JavaScript中的JSON数据时遇到问题,特别是在将数据用作数组以及访问和迭代各个值方面。JSON文件的结构如下: 我希望能够将其转换为如下所示的JavaScript数组,以便可以迭代它并按顺序提取每个位置的值: 有人对如何实现这一目标有任何建议吗?我已经尝试了以下方法,但是它在JSON中选择了“类型”,而不仅仅是值: 任何帮助,建议,建议或更正将不胜感激。 问题答案:

  • 我关注了这篇文章:如何将JavaScript数组信息导出到csv(在客户端)?将嵌套js数组写入csv文件。 该数组如下所示:

  • 问题内容: 我正在尝试将字符串转换为整数数组,以便随后可以对它们执行数学运算。我在以下代码方面遇到麻烦: 这给了我一个“无法转换的类型”错误,要求:int []发现:char我还尝试了其他一些方法,例如Character.getNumericValue,并且直接对其进行了直接分配,而没有进行任何修改。在那种情况下,无论我使用哪种转换方法或(!)字符串的实际值是什么,它总是输出相同的垃圾“ [I @

  • 问题内容: 我正在尝试将JSON字符串解码为数组,但出现以下错误。 致命错误:不能在第6行的C:\ wamp \ www \ temp \ asklaila.php中将stdClass类型的对象用作数组 这是代码: 问题答案: 根据文档,您需要指定是否要使用关联数组而不是中的对象,这是代码:

  • 我是Java8的新手,不能使用流将一个数组映射到另一个二维数组。 和包含键的第二个数组。 0表示:从模式中取0元素 1表示:从模式中取1元素,以此类推 从这两个数组中,我想生成另一个二维数组。在这种情况下,结果如下所示: 请帮忙