本文介绍了PHP-如何从多维数组中回显值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有:

$request = Array
(
    [ID] => 2264
    [SUCCESS] => Array
        (
            [0] => Array
                (
                    [MESSAGE] => Service Details
                    [LIST] => Array
                        (
                            [retail_name] =>
                            [credit_admin] => FREE
                            [credit] => 0
                            [discount_admin] => 0
                            [discount] => 0
                            [cartdiscount] => 0

如果我这样做:

echo $request[ID];                            // it says: 2264
echo $request[SUCCESS];                       // it says: Array
echo $request[SUCCESS][0][MESSAGE];           // it says: Service Details

但是我需要回显 credit,如果我这样做:

But I need to echo "credit" and if I do:

echo $request[SUCCESS][0][LIST];           // I get ERROR
echo $request[SUCCESS][0][LIST][credit];   // I get ERROR

我不明白为什么?我该怎么做?
谢谢

I dont understand why? How can I do it?Thank you

推荐答案

以前,如果在该名称下未定义任何常量,则每个不带引号的字符串都被视为字符串,但是从 PHP 7.2 起,现在发出的错误级别为 E_WARNING

Previously every string without quotes was treated like string if there was no Constant defined under this name, but from PHP 7.2 now issues error of level E_WARNING. Why is $foo[bar] wrong?

PHP Warning:  Use of undefined constant test - assumed 'test' (this will throw an Error in a future version of PHP)

在此示例中,列表被视为构造。

In this example list is treated as construct list(). Using bare strings in associative arrays as keys is deprecated and should be avoided.

正确的方法是使用引号(单引号或双引号)来调用它:

Correct way is to call it with quotes (single or double):

echo $request['SUCCESS'][0]['LIST'];
echo $request['SUCCESS'][0]['LIST']['credit'];

这篇关于PHP-如何从多维数组中回显值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 17:25