首页 \ 问答 \ 从PHP中获取来自JSON数组的值(Get value from JSON array in PHP)

从PHP中获取来自JSON数组的值(Get value from JSON array in PHP)

我试图从这个PHP变量中的下面的JSON数组中获取值。

这是一个数组的var_dump:

array(3) {
  ["id"]=>
  string(2) "14"
  ["css"]=>
  string(400) ""
  ["json"]=>
  string(4086) "
            {
                "Canvas": [
                    {
                        "MainObjects": {
                            "After Participation": {
                                "afterParticipationHeader": "Thank you!"
                            },
                            "Invite Friends": {
                                "InviteHeadline": "",
                                "InviteMsg": "",
                                "InviteImg": ""
                            }
                        },
                        "QuizModule": {
                            "Questions": [],
                            "Submit_Fields": [
                                {
                                    "label": "Name",
                                    "name": "txtName",
                                    "value": true
                                }
                            ]
                        }
                    }
                ]
            }"
        }

我能够在PHP中获得[“json”]的值,如下所示:

$json = $data[0]['json'];

但是,如何从“json”内的数组中获取值,如“AfterParticipationHeader”。 和“Submit_Fields”?


I'm trying to get the value from this following JSON array in a PHP variable.

This is a var_dump of the array:

array(3) {
  ["id"]=>
  string(2) "14"
  ["css"]=>
  string(400) ""
  ["json"]=>
  string(4086) "
            {
                "Canvas": [
                    {
                        "MainObjects": {
                            "After Participation": {
                                "afterParticipationHeader": "Thank you!"
                            },
                            "Invite Friends": {
                                "InviteHeadline": "",
                                "InviteMsg": "",
                                "InviteImg": ""
                            }
                        },
                        "QuizModule": {
                            "Questions": [],
                            "Submit_Fields": [
                                {
                                    "label": "Name",
                                    "name": "txtName",
                                    "value": true
                                }
                            ]
                        }
                    }
                ]
            }"
        }

I am able to get the values for ["json"] in PHP like:

$json = $data[0]['json'];

But how do I get the value from from the array inside "json", like "AfterParticipationHeader". And "Submit_Fields" ?


原文:https://stackoverflow.com/questions/17995877
更新时间:2023-12-07 13:12

最满意答案

我想我找到了你的问题:从sm.add_constant文档中,你需要设置一个名为has_constant的参数来设置add (默认为skip )。

has_constant:str {'raise','add','skip'}如果``data''已经有一个常量的行为。 默认将返回数据而不添加另一个常量。 如果'raise',如果存在常量则会引发错误。 使用'add'将复制常量(如果存在)。 对结构化或重新排列没有影响。 在这种情况下,没有检查常数。

基本上对于循环的迭代,您的变量time在子集中是常量,因此函数没有添加常量,因此RollOLS.params只有2个条目。

temp
Out[12]: 
        time   X     Y      a           b1           b2
541  0.16182  13  20.0  19.49      3.15289 -1.26116e-05
542  0.16182  14  20.0     20            0  7.10543e-15
543  0.16182  15  20.0     20 -7.45058e-09            0

sm.add_constant(temp.loc[:,['time','X']])
Out[13]: 
        time   X
541  0.16182  13
542  0.16182  14
543  0.16182  15

sm.add_constant(temp.loc[:,['time','X']], has_constant = 'add')
Out[14]: 
     const     time   X
541      1  0.16182  13
542      1  0.16182  14
543      1  0.16182  15

因此,如果你在sm.add_constant函数中有has_constant = 'add' ,那么错误就会消失,但是在解释变量中你会有两个线性相关的列,这使得矩阵不可逆,因此回归没有意义。


I think I found your problem: from the documentation of sm.add_constant, there is an argument called has_constant that you need to set to add (the default is skip).

has_constant : str {'raise', 'add', 'skip'} Behavior if ``data'' already has a constant. The default will return data without adding another constant. If 'raise', will raise an error if a constant is present. Using 'add' will duplicate the constant, if one is present. Has no effect for structured or recarrays. There is no checking for a constant in this case.

Essentially for that iteration of the loop your variable time was constant in the subset, hence the function did not add a constant and as a result RollOLS.params had only 2 entries.

temp
Out[12]: 
        time   X     Y      a           b1           b2
541  0.16182  13  20.0  19.49      3.15289 -1.26116e-05
542  0.16182  14  20.0     20            0  7.10543e-15
543  0.16182  15  20.0     20 -7.45058e-09            0

sm.add_constant(temp.loc[:,['time','X']])
Out[13]: 
        time   X
541  0.16182  13
542  0.16182  14
543  0.16182  15

sm.add_constant(temp.loc[:,['time','X']], has_constant = 'add')
Out[14]: 
     const     time   X
541      1  0.16182  13
542      1  0.16182  14
543      1  0.16182  15

So the error is going away if you had has_constant = 'add' in the sm.add_constant function, but you would have two linearly dependent columns in the explanatory variables, which makes the matrix not invertible hence the regression would not make sense.

相关问答

更多
  • IndexError的原因是您使用变量名称i两次( for i in range(n) for i in range(rand-1,rand) )。 当调用x[i+1] ,我假设您期望外部for循环中的i ,但其值在内部意外更改。 尝试为另一个for循环使用不同的索引变量名称。 The reason for the IndexError is that you are using the variable name i twice (for i in range(n) and for i in range( ...
  • 我想我找到了你的问题:从sm.add_constant的文档中,你需要设置一个名为has_constant的参数来设置add (默认为skip )。 has_constant:str {'raise','add','skip'}如果``data''已经有一个常量的行为。 默认将返回数据而不添加另一个常量。 如果'raise',如果存在常量则会引发错误。 使用'add'将复制常量(如果存在)。 对结构化或重新排列没有影响。 在这种情况下,没有检查常数。 基本上对于循环的迭代,您的变量time在子集中是常量,因 ...
  • 您正在访问数组的边界之外。 在你的内循环中,你有一行binary = np.delete(binary, (loser), axis=0) 假设第一次通过,最小值的索引( loser )是9(10个元素数组的最后一个元素)。 您的binary数组长度为10.您从数组中删除该条目。 二进制数组现在长度为9. value数组中最小值的索引没有改变! 所以,下一次,当你试图通过print("binary:",(binary[loser,]))访问元素9时,你会得到一个错误。 至于解决你的问题,你正在使用从一个数组 ...
  • 你需要在N1而不是N上设置停止条件: while N1 < len(test): Your need to put stop condition on N1 instead of N: while N1 < len(test):
  • 至少其中一个room.x1 + 1 , room.x2 , room.y1 + 1和room.y2超出了你的地图。 检查地图大小或限制访问权限。 选项1: class RoomOutsideError(Exception): pass # ... def create_room(room): global map #set all tiles inside the room to passable # check for limits and raise excepti ...
  • 您需要检查字符串的长度,例如: letterCount+2 <= len(s) 即 s = 'oobobodobooobobobobabobbobbobobbobbobhbxbobbk' vowelCount = 0 letterCount = 0 pointer = s for pointer in s: print(pointer) if pointer == 'b': print (str(letterCount) + '. betű B' ) ...
  • 您的安装似乎有些问题 - 很难看出线路如何 while logger: 会生成一个IndexError 。 因此,从系统中删除所有.pyc和.pyo文件(包括在stdlib文件夹中),然后重试。 确保您的模块没有与标准库中的任何模块同名。 另外,您使用的是哪个版本的Python,以及在哪个平台上? 更新:如果您在C ++程序中使用嵌入式或使用C扩展,很可能某些C或C ++代码会破坏内存,从而导致IndexError在意外的地方。 你能在纯Python环境中重现吗? 如果没有,我担心问题可能出在C / C ...
  • 你在range (0, len(chests))进行迭代range (0, len(chests)) ,然后在线路visits[i] += 1中得到错误。 看起来像chests和visits长度不等。 我看不到任何appends到visits列表,所以它基本上是空的。 此外,您正在混合嵌套循环中的计数器: for i in range (0, len(chests)): ... # you override the i value here for i in range(0, len ...
  • 你不是以正确的方式调用multipolyfit 。 docstring建议它应该用作multipolyfit.multipolyfit(xs,y,deg) ,其中xs : array_like, shape (M, k)是你的hour , day temps 。 这表明它们需要作为单个数组传递,如下所示: multipolyfit.multipolyfit(np.vstack((hour,day,temps)).T ,load,3) You aren't calling multipolyfit in t ...
  • 很高兴看到一些聪明的蔬菜编程。 首先,你的问题。 就像@Vasiliy所说,你有3个指数。 n ,因为你可以用你的条件来保护它。 1很好,因为enumerate总是生成2件事。 那只是留下了m 。 这是你的问题。 假设你在strlist有N元素。 对于strlist每个元素e ,您可以对其应用split() 。 e.split()的元素数量并不总是等于N m守时条件是针对N而不是len(e.split()) ,因此指数超出范围。 要解决此问题,请首先拆分字符串,然后循环遍历它。 当你在它的时候,不妨完全摆脱 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)