简体   繁体   English

如何将 JavaScript object 转换为 PHP 数组并获取原始字符串?

[英]How to convert a JavaScript object to a PHP array and get the raw string?

In my application I need to convert a JavaScript Object to a raw PHP array returned as text string.在我的应用程序中,我需要将 JavaScript Object 转换为以文本字符串形式返回的原始 PHP 数组。 I would do this calling an API.我会调用 API 来做到这一点。 I don't need to manupulate the array.我不需要操纵数组。 In my JavaScript application, I just need the plain PHP object string as showed below.在我的 JavaScript 应用程序中,我只需要简单的 PHP object 字符串,如下所示。 (Like using this: https://wtools.io/convert-js-object-to-php-array ) (比如使用这个: https://wtools.io/convert-js-object-to-php-array

The JavaScript object is: JavaScript object 是:

{
"name":"A Name",
"_typography": {
  "color": {
    "hex":"#2196f3"
  },
  "text-transform":"uppercase",
  "font-weight":"600"
  }
}

How I send the JavaScript Object to the PHP function我如何将 JavaScript Object 发送到 PHP ZC1C425268E68385D1AB5074F1C

JSON.stringify(jsObject);

What I need as string output:我需要的字符串 output:

[
"name" => "A Name",
"_typography" => [
  "color" => [
    "hex" => "#2196f3"
  ],
  "text-transform" => "uppercase",
  "font-weight" =>"600"
  ]
]

What I tried is to first json_decode the JavaScript object to a PHP object and then try to return it as a plain string back.我尝试的是首先将json_decode object json_decode 到 PHP ZA8CFDE6331BD59EB2AC96F891 然后尝试将其作为字符串返回。 But no chance.但没有机会。 Either I get a Js Object back or something else entirely.要么我得到一个 Js Object 要么完全得到其他东西。 I have no more ideas.我没有更多的想法了。

My try我的尝试

function convert_js_object_to_php(WP_REST_Request $request) {

    // Getting the JS object
    $object = $request->get_header('js_object');

    // Decoding it to a PHP object
    $output = json_decode($object);

    if ($output) {
        return strval($output);
        // return $output (Here I also get the JavaScript object again)
        // return implode($output) (I don't get the plain PHP array)
    }

    return false;
}

Do you have any idea?你有什么主意吗?

Is this what you are trying to achieve?这是你想要达到的目标吗?

$incoming is the jsonString coming from the javascript code $incoming是来自 javascript 代码的 jsonString

$incomming = '{
    "name":"A Name",
    "_typography": {
      "color": {
        "hex":"#2196f3"
      },
      "text-transform":"uppercase",
      "font-weight":"600"
      }
    }';

$a = json_decode($incomming, true);
echo var_export($a);

RESULT结果

array (
  'name' => 'A Name',
  '_typography' => 
  array (
    'color' => 
    array (
      'hex' => '#2196f3',
    ),
    'text-transform' => 'uppercase',
    'font-weight' => '600',
  ),
)

Only changes are that you should add true to json_decode and use var_export to get the php array string.唯一的变化是您应该将 true 添加到 json_decode 并使用 var_export 来获取 php 数组字符串。 If you wanted to get square bracket you could write some custom string generator code.如果你想得到方括号,你可以编写一些自定义字符串生成器代码。

function convert_js_object_to_php() {

    // Getting the JS object
    $object = $request->get_header('js_object');

    // Decoding it to a PHP object
    $output = json_decode($object, true);
    if ($output) {
        return var_export($output, true);
    }

    return false;
}

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

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