简体   繁体   English

PHP:如何在 Javascript 中将 json 解码回数组形式?

[英]PHP: How do I can decode a json back into array form in Javascript?

I've an array in my php called as $tempoHeader , for example:我的 php 中有一个名为$tempoHeader ,例如:

Example Array $tempoHeader示例数组 $tempoHeader

-------
Array
(
   [0] => red
   [1] => green
   [2] => yellow
   [3] => blue
)

I want to send it into my external javascript , so I do this one in my php code :我想将它发送到我的外部javascript 中,所以我在我的php 代码中执行了这个:

My PHP: header.php我的 PHP:header.php

$tempoHeader = array_diff_assoc($modelData, $data);
<script type="text/javascript">var header = <?php echo json_encode($tempoHeader); ?>;</script>
<script type="text/javascript" src="upload_header.js"></script>

In my javascript , I've try to console.log(header);在我的javascript 中,我尝试使用console.log(header); and I got this in my console我在我的控制台中得到了这个

Array [ "red", "green", "yellow", "blue" ]

My JS: upload_header.js我的 JS:upload_header.js

$(window).load(function() {
console.log(header);                    //this work and print out the header json
   var myObject = JSON.parse(header);
   var myArray = jQuery.parseJSON(header);
   console.log(myArray);                   //didn't print out anything
   console.log(myObject);                  //didn't print out anything
});

How do I can decode this json back as $tempoHeader array form?如何将此 json 解码为 $tempoHeader 数组形式?

Note : I've read this issue jQuery JSON Decode ( PHP to Javascript) , and I've try the suggestion such as jQuery.parseJSON(header);注意:我已经阅读了这个问题jQuery JSON Decode ( PHP to Javascript) ,并且我已经尝试了诸如jQuery.parseJSON(header);这样的建议jQuery.parseJSON(header); , JSON.parse(header); , JSON.parse(header); but I got nothing但我一无所获

$res = json_decode( $tempoHeader, true );
echo $res->array_name[index];

I hope this help我希望这有帮助

header already is an array, it is not json / a string and you do not need to parse it. header已经是一个数组,它不是 json / 一个字符串,你不需要解析它。

You send this to javascript:您将其发送给 javascript:

var header = <?php echo json_encode($tempoHeader); ?>;    

So you already have an array in javascript as you have noticed:因此,正如您所注意到的,您已经在 javascript 中有一个数组:

Array [ "red", "green", "yellow", "blue" ]

And then you try to parse that as json:然后您尝试将其解析为 json:

var myObject = JSON.parse(header);

That is not possible nor is it necessary: json is a string and JSON.parse() expects a string as its parameter.这是不可能的,也不是必需的:json 是一个字符串,而JSON.parse()需要一个字符串作为其参数。 You cannot use it on an array.您不能在数组上使用它。

To use your variable, you can simply use header[0] (red), etc. or loop over it like you do with any other javascript array.要使用您的变量,您可以简单地使用header[0] (红色)等,或者像使用任何其他 javascript 数组一样循环遍历它。

The Problem here is that when you executes your JavaScript, your PHP Script was already executed, to "send a variable back to php" you have to create a new Request.这里的问题是,当您执行 JavaScript 时,您的 PHP 脚本已经执行,要“将变量发送回 php”,您必须创建一个新的请求。

Mostly it is done via Ajax OR you can add your Array in a hidden input field and "send" it with a form.大多数情况下,它是通过 Ajax 完成的,或者您可以将您的数组添加到隐藏的输入字段中并使用表单“发送”它。

So you upload_header.js would look like所以你 upload_header.js 看起来像

   $(function () {

        $.ajax('update_header.php', {
            method: 'POST',
            data: {
                'header': JSON.stringify(header)
            }
        })
    });

and in your "update_header.php" you would recieve the new header with在你的“update_header.php”中,你会收到新的标题

$newHeader = json_decode($_POST['header'],true);

hope this helps;)希望这可以帮助;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM