dongwen6743 2016-05-04 17:01
浏览 46

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

I am calling a PHP file using http.post, passing a json object in the process. I have managed to retrieve the object from within the PHP and have attached the dump below. All I now need is to retrieve 'name', 'email' and 'message' strings from the array but am finding this difficult as not used to 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>

The PHP code which retrieves the object in the first place is as follows:

$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>";

How can I access the 'name', 'email' and 'message' strings please?

  • 写回答

2条回答 默认 最新

  • duanjian5059 2016-05-04 17:22
    关注

    First of all your array isnt a valid one... it should be something like this:

    BTW remove the $ from inside the arrays.

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

    Now to get let say the first name, since it is a subarray (so an array inside another array).

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

    Hope this helps.

    UPDATE

    I didn't mean not valid, it is a bad practice to use an array like that.

    评论

报告相同问题?

悬赏问题

  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来