从多维数组中获取特定信息

问题描述 投票:0回答:2

我有这个多维数组,我从JSON字符串解码,现在我想输出一些数据,但我不能?如果它是单个阵列,那不是问题。

我已经尝试过一些foreach循环,但是我无法输出我想要的数据。我一直收到非法的偏移错误。

$someArray = array (
    'result' => 'success',
    'response' => 
    array (
      'server_info' => 
      array (
        'status' => 'success',
        'ipaddress' => '123.123.123.123',
        'hostname' => 'hostname.example.com',
        'template' => 'linux-centos-6.5-x86_64-min-gen2-v1',
        'hdd' => '32212254720',
        'memory' => '1073741824',
        'country' => 'FR',
        'city' => 'Paris',
        'plan' => 'Plan 02',
        'rootpassword' => '1q2w3e4r5t',
      ),
      'server_state' => 
      array (
        'status' => 'success',
        'state' => 'online',
        'mainipaddress' => '123.123.123.123',
        'ipaddresses' => '123.123.123.123',
        'bandwidth' => '2199023255552,112133881,2198911121671,0',
        'hdd' => '32212254720,0,32212254720,0',
        'pae' => 'off',
        'bootorder' => 'cd',
        'iso' => '',
      ),
      'templates' => 
      array (
        'linux-centos-7.1503.01-x86_64-minimal-gen2-v1' => 
        array (
          'name' => 'centos',
          'display' => 'CentOS 7.1503.01 64bits minimal  ',
        ),
        'linux-clearos-6.3-x86_64-gen2-v1' => 
        array (
          'name' => 'clearos',
          'display' => 'ClearOS 6.3 64bits  ',
        ),
        'linux-debian-6.0.6-x86_64-min-gen2-v1' => 
        array (
          'name' => 'debian',
          'display' => 'Debian 6.0.6 64bits ',
        ),
      ),
      'one_click_apps' => 
      array (
        'linux-ubuntu-14.04-server-x86_64-min-gen2-v1-oneapp-webuzo' => 
        array (
          'name' => 'Webuzo',
          'display' => 'Webuzo',
          'time' => '2',
          'version' => '2.5',
          'desc' => 'Webuzo is a LAMP STACK and a Single User Control Panel, it simplifies the development and deployment of your project by supporting more than 400 applications and 1115 classes. ',
          'steps' => 'You will be able to access the Webuzo setup and dashboard by entering the following syntax in your web browser: http://IP:2004',
          'os' => 'Ubuntu 14.04  64bits ',
        ),
      ),
      'iso_images' => 
      array (
        0 => 'FreeBSD-9.1-RELEASE-amd64-disc1',
        1 => 'CentOS-7.0-1406-x86_64-Minimal',
        2 => 'FreeBSD-9.3-RELEASE-amd64-bootonly',
        3 => 'FreeBSD-10.1-RELEASE-amd64-disc1',
        4 => 'FreeBSD-8.4-RELEASE-amd64-disc1',
      ),
      'log' => 
      array (
        0 => 
        array (
          'log' => '[Reinstall] - Virtual server: 12345 - App: Drupal on ubuntu 14.04 64bits ',
          'time' => '2042-04-02 16:20:42',
        ),
        1 => 
        array (
          'log' => '[Reinstall] - Virtual server: 12345 - OS: Ubuntu 14.04 server 64bits ',
          'time' => '2042-04-20 04:20:42',
        ),
        2 => 
        array (
          'log' => '[Change hostname] - Virtual server: 12345 - Hostname: new.hostname.com',
          'time' => '2042-04-02 04:20:00',
        ),
      ),
      'server_billing' => 
      array (
        'reg_date' => '2015-03-12',
        'next_due_date' => '2015-04-12',
        'payment_method' => 'paypal',
        'first_payment_amount' => '0.00',
        'recurring_amount' => '0.00',
        'billing_cycle' => 'Monthly',
        'status' => 'Active',
      ),
      'server_install' => 0,
    ),
);


  $array = $someArray; 
  foreach($array as $key => $value) {
    echo $array[$key]["status"] .  "<br>";
  }

</code>

这是我从print_r得到的:

Array
(
    [result] => success
    [response] => Array
        (
            [server_info] => Array
                (
                    [status] => success
                    [ipaddress] => 123.123.123.123
                    [hostname] => hostname.example.com
                    [template] => linux-centos-6.5-x86_64-min-gen2-v1
                    [hdd] => 32212254720
                    [memory] => 1073741824
                    [country] => FR
                    [city] => Paris
                    [plan] => Plan 02
                    [rootpassword] => 1q2w3e4r5t
                )

            [server_state] => Array
                (
                    [status] => success
                    [state] => online
                    [mainipaddress] => 123.123.123.123
                    [ipaddresses] => 123.123.123.123
                    [bandwidth] => 2199023255552,112133881,2198911121671,0
                    [hdd] => 32212254720,0,32212254720,0
                    [pae] => off
                    [bootorder] => cd
                    [iso] => 
                )

            [templates] => Array
                (
                    [linux-centos-7.1503.01-x86_64-minimal-gen2-v1] => Array
                        (
                            [name] => centos
                            [display] => CentOS 7.1503.01 64bits minimal  
                        )

                    [linux-clearos-6.3-x86_64-gen2-v1] => Array
                        (
                            [name] => clearos
                            [display] => ClearOS 6.3 64bits  
                        )

                    [linux-debian-6.0.6-x86_64-min-gen2-v1] => Array
                        (
                            [name] => debian
                            [display] => Debian 6.0.6 64bits 
                        )

                )

            [one_click_apps] => Array
                (
                    [linux-ubuntu-14.04-server-x86_64-min-gen2-v1-oneapp-webuzo] => Array
                        (
                            [name] => Webuzo
                            [display] => Webuzo
                            [time] => 2
                            [version] => 2.5
                            [desc] => Webuzo is a LAMP STACK and a Single User Control Panel, it simplifies the development and deployment of your project by supporting more than 400 applications and 1115 classes. 
                            [steps] => You will be able to access the Webuzo setup and dashboard by entering the following syntax in your web browser: http://IP:2004
                            [os] => Ubuntu 14.04  64bits 
                        )

                )

            [iso_images] => Array
                (
                    [0] => FreeBSD-9.1-RELEASE-amd64-disc1
                    [1] => CentOS-7.0-1406-x86_64-Minimal
                    [2] => FreeBSD-9.3-RELEASE-amd64-bootonly
                    [3] => FreeBSD-10.1-RELEASE-amd64-disc1
                    [4] => FreeBSD-8.4-RELEASE-amd64-disc1
                )

            [log] => Array
                (
                    [0] => Array
                        (
                            [log] => [Reinstall] - Virtual server: 12345 - App: Drupal on ubuntu 14.04 64bits 
                            [time] => 2042-04-02 16:20:42
                        )

                    [1] => Array
                        (
                            [log] => [Reinstall] - Virtual server: 12345 - OS: Ubuntu 14.04 server 64bits 
                            [time] => 2042-04-20 04:20:42
                        )

                    [2] => Array
                        (
                            [log] => [Change hostname] - Virtual server: 12345 - Hostname: new.hostname.com
                            [time] => 2042-04-02 04:20:00
                        )

                )

            [server_billing] => Array
                (
                    [reg_date] => 2015-03-12
                    [next_due_date] => 2015-04-12
                    [payment_method] => paypal
                    [first_payment_amount] => 0.00
                    [recurring_amount] => 0.00
                    [billing_cycle] => Monthly
                    [status] => Active
                )

            [server_install] => 0
        )

)

我想输出From:

Server_info
-status
-ipaddress
-and so on
server_state
-same here

但我保持非法偏移错误或输出NULL

php arrays json
2个回答
1
投票

我没有阅读整个代码,但你打开这样的子代码:

$subarray_value = $array['subarray']['deeper']['even_more_below'];

0
投票

你只需要迭代$array['response']而不是$array本身,因为它在$ array ['result']中找不到“status”。

即。而不是

foreach($array as $key => $value) {
    echo $array[$key]["status"] .  "<br>";
  }

你应该做,

if(isset($array['response'])) {
    foreach($array['response'] as $key => $value) {
        echo $value[$key]["status"] .  "<br>";
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.