如何在PHP中的多维数组下面实现?

时间:2015-10-16 10:12:42

标签: php multidimensional-array

<?php
[USED] => Array
(
        [USER] => Array
        (
                [@attributes] => Array
                (
                        [NAME] => locuz
                        [HOST] => srpth1cn03.local
                        [IP] => 10.106.2.48
                        [USED_LICENSES] => 1
                        [LOGIN_TIME] => 2014-12-10 07:34
                        [CHECKOUT_TIME] => 2014-12-10 07:34
                )

        )

)

我想将上面的数组改为:

[USED] => Array
(
        [USER] => Array
        (
                [0] => Array
                (
                        [@attributes] => Array
                        (
                                [NAME] => rdtank
                                [HOST] => it30992
                                [IP] => 10.106.21.134
                                [DENIED_LICENSES] => 1
                                [LOGIN_TIME] => 2014-12-09 15:55
                                [DENIAL_TIME] => 2014-12-09 15:55
                        )

                )

        )
)

2 个答案:

答案 0 :(得分:0)

在保存属性之前设置[],如下所示。

foreach($all_attributes as $attributes){
  $sample_array['USED']['USER'][] = $attributes;
}

答案 1 :(得分:0)

您编写的代码似乎是2数组转储的结果。

所以如果你需要在PHP中设置包含数据的2个变量,你应该使用这个代码

<?php

    $used = array(
                'users' => array(
                    array(
                        'name' => 'locuz',
                        'host' => 'srpth1cn03.local',
                        'ip' => '10.106.2.48',
                        'used_licenses' => 1,
                        'login_time' => '2014-12-10 07:34',
                        'checkout_time' => '2014-12-10 07:34'
                    )
                )
            );

    $denied = array(
                  'users' => array(
                      array(
                          'name' => 'rdtank',
                          'host' => 'it30992',
                          'ip' => '10.106.21.134',
                          'denied_licenses' => 1,
                          'login_time' => '2014-12-09 15:55',
                          'denial_time' => '2014-12-09 15:55'
                      )
                  )
              );

希望这会有所帮助。