从PHP文件中传递的参数检索数组元素

时间:2016-05-04 17:01:18

标签: php arrays json

我使用http.post调用PHP文件,在进程中传递json对象。 我已经设法从PHP中检索对象并在下面附加了转储。我现在需要的是检索姓名','电子邮件'和'消息'数组中的字符串,但发现这很困难,因为不习惯PHP。

            Connected successfully<pre>string(2467) "Array
            (
                [name] => Array
                    (
                        [$viewValue] => testing one two
                        [$modelValue] => testing one two
                        [$validators] => Array
                            (
                            )

                        [$asyncValidators] => Array
                            (
                            )

                        [$parsers] => Array
                            (
                            )

                        [$formatters] => Array
                            (
                                [0] => 
                            )

                        [$viewChangeListeners] => Array
                            (
                            )

                        [$untouched] => 
                        [$touched] => 1
                        [$pristine] => 
                        [$dirty] => 1
                        [$valid] => 1
                        [$invalid] => 
                        [$error] => Array
                            (
                            )

                        [$name] => fullName
                        [$options] => 
                    )

                [email] => Array
                    (
                        [$viewValue] => test@onetwo.com
                        [$modelValue] => test@onetwo.com
                        [$validators] => Array
                            (
                            )

                        [$asyncValidators] => Array
                            (
                            )

                        [$parsers] => Array
                            (
                            )

                        [$formatters] => Array
                            (
                                [0] => 
                            )

                        [$viewChangeListeners] => Array
                            (
                            )

                        [$untouched] => 
                        [$touched] => 1
                        [$pristine] => 
                        [$dirty] => 1
                        [$valid] => 1
                        [$invalid] => 
                        [$error] => Array
                            (
                            )

                        [$name] => email
                        [$options] => 
                    )

                [message] => Array
                    (
                        [$viewValue] => testing testing
                        [$modelValue] => testing testing
                        [$validators] => Array
                            (
                            )

                        [$asyncValidators] => Array
                            (
                            )

                        [$parsers] => Array
                            (
                            )

                        [$formatters] => Array
                            (
                                [0] => 
                            )

                        [$viewChangeListeners] => Array
                            (
                            )

                        [$untouched] => 
                        [$touched] => 1
                        [$pristine] => 
                        [$dirty] => 1
                        [$valid] => 1
                        [$invalid] => 
                        [$error] => Array
                            (
                            )

                        [$name] => message
                        [$options] => 
                    )

            )
            "
            <br /><br />Array
            (
                [name] => Array
                    (
                        [$viewValue] => testing one two
                        [$modelValue] => testing one two
                        [$validators] => Array
                            (
                            )

                        [$asyncValidators] => Array
                            (
                            )

                        [$parsers] => Array
                            (
                            )

                        [$formatters] => Array
                            (
                                [0] => 
                            )

                        [$viewChangeListeners] => Array
                            (
                            )

                        [$untouched] => 
                        [$touched] => 1
                        [$pristine] => 
                        [$dirty] => 1
                        [$valid] => 1
                        [$invalid] => 
                        [$error] => Array
                            (
                            )

                        [$name] => fullName
                        [$options] => 
                    )

                [email] => Array
                    (
                        [$viewValue] => test@onetwo.com
                        [$modelValue] => test@onetwo.com
                        [$validators] => Array
                            (
                            )

                        [$asyncValidators] => Array
                            (
                            )

                        [$parsers] => Array
                            (
                            )

                        [$formatters] => Array
                            (
                                [0] => 
                            )

                        [$viewChangeListeners] => Array
                            (
                            )

                        [$untouched] => 
                        [$touched] => 1
                        [$pristine] => 
                        [$dirty] => 1
                        [$valid] => 1
                        [$invalid] => 
                        [$error] => Array
                            (
                            )

                        [$name] => email
                        [$options] => 
                    )

                [message] => Array
                    (
                        [$viewValue] => testing testing
                        [$modelValue] => testing testing
                        [$validators] => Array
                            (
                            )

                        [$asyncValidators] => Array
                            (
                            )

                        [$parsers] => Array
                            (
                            )

                        [$formatters] => Array
                            (
                                [0] => 
                            )

                        [$viewChangeListeners] => Array
                            (
                            )

                        [$untouched] => 
                        [$touched] => 1
                        [$pristine] => 
                        [$dirty] => 1
                        [$valid] => 1
                        [$invalid] => 
                        [$error] => Array
                            (
                            )

                        [$name] => message
                        [$options] => 
                    )

            )
            </pre>

首先检索对象的PHP代码如下:

$data = json_decode(file_get_contents('php://input'), TRUE);
$text = print_r($data,true);

echo "<pre>";
var_dump($text);
echo "<br /><br />";
print_r($text);
echo "</pre>";

如何访问&#39;姓名&#39;,&#39;电子邮件&#39;和&#39;消息&#39;字符串好吗?

2 个答案:

答案 0 :(得分:0)

首先你的数组不是有效的...它应该是这样的:

BTW从数组内部删除$。

$newarr = Array(
  "name" => Array(
    "first_name" => "Alex",
    "last_name" => "Gonzalez"
     ),
  "email" => Array(),
  "other_sub_array" => Array()
);

现在让我们说第一个名字,因为它是一个子阵列(所以在另一个数组中是一个数组)。

echo $newarr['name']['first_name'];
// Result: Alex

希望这有帮助。

<强>更新

我并不意味着无效,使用像这样的数组是不好的做法。

答案 1 :(得分:0)

与其他答案不同,您的数组有效。我假设您正在使用一些框架来生成您正在阅读的相对奇怪的json。但到了这一点,您可以通过以下方式获取它们:

//note I used single quotes so that the dollar sign isn't evaluated to a php variable
$data['name']['$modelValue'];
$data['message']['$modelValue'];
$data['email']['$modelValue'];