简体   繁体   English

在php中访问多维json数组的正确方法是什么?

[英]What is the right way to access multidimensional json array in php?

I'm working on cookies that contain multidimensional JSON object array and I have bit of a problem accessing them. 我正在处理包含多维JSON对象数组的cookie,但是访问它们时遇到了一些问题。 Kindly help me. 请帮助我。

Here's a look at my problem: I access the values from the html element and encode it before storing it into a variable, 这是我的问题:在存储到变量之前,我从html元素访问值并将其编码,

var cookie_items = JSON.stringify({id: value, quantity: 1});

then I push this object into an array, 然后我将此对象推入数组,

cookie_array.push(cookie_items);

finally, I push the cookie_array into another array with the associative name 'all', 最后,我将cookie_array推入另一个名为“ all”的数组,

final_cookie_array['all'] = cookie_array;

When I log final_cookie_array in the browser console, the output looks like this: 当我在浏览器控制台中登录final_cookie_array时,输出如下所示:

Object {all: Array[2]}
  all: Array[2]
    0: "{"id":"6","quantity":1}"
    1: "{"id":"2","quantity":1}"

When I echo the cookie array in PHP, ie, $_COOKIE['cookie_name']; 当我在PHP中回显cookie数组时,即$ _COOKIE ['cookie_name']; it outputs the following: 它输出以下内容:

[object Object]

But when I try echoing $_COOKIE['cookie_name']['all']; 但是,当我尝试回显$ _COOKIE ['cookie_name'] ['all'];时, nothing gets displayed. 什么都不会显示。

Can somebody please help me deal with this? 有人可以帮我解决这个问题吗? Thanks. 谢谢。

Try like example below 尝试下面的例子

<?php
   $json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
   $arr = json_decode($json,true);
   var_dump($arr);
?>

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

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